Android:为什么 setVisibility(View.GONE);或 setVisibility(View.INVISIBLE);不工作
我希望我的 DatePicker
和按钮在开始时不可见。当我按下魔术按钮时,我想 setVisibility(View.Visible);
这里的问题是,当我 setVisibility(View.GONE)
或 setVisibility(View.INVISIBLE)
没有任何变化并且组件仍然可见时。
final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);
dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
TextView txt2 = (TextView) findViewById(R.id.txt2);
txt2.setText("You selected " + dp2.getDayOfMonth()
+ "/" + (dp2.getMonth() + 1) + "/" + dp2.getYear());
}
});
I want my DatePicker
and the button to be invisible in the begining. And when I press my magic button I want to setVisibility(View.Visible);
The problem here is when I setVisibility(View.GONE)
or setVisibility(View.INVISIBLE)
nothing changes and the component is still visible.
final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);
dp2.setVisibility(View.GONE);
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
btn2.setVisibility(View.INVISIBLE);
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
TextView txt2 = (TextView) findViewById(R.id.txt2);
txt2.setText("You selected " + dp2.getDayOfMonth()
+ "/" + (dp2.getMonth() + 1) + "/" + dp2.getYear());
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我看到很多事情都是错误的。对于初学者来说,您没有定义魔术按钮,也没有针对它的事件处理程序。
另外,您不应该使用:
仅使用两者之一。来自 Android 文档:
在您的示例中,您将使用
View.INVISIBLE
赋值覆盖View.GONE
赋值。尝试将:替换
为:
对于其他小部件类似:
I see quite a few things wrong. For starters, you don't have your magic button defined and there is no event handler for it.
Also you shouldn't use:
Use only one of the two. From Android documentation:
In your example, you are overriding the
View.GONE
assignment with theView.INVISIBLE
one.Try replacing:
with:
Similarly for other widgets:
今天,我遇到了一个场景,我正在执行以下操作:
在下一帧上,我正在其他地方执行
if
检查该视图的可见性状态。你猜怎么着?以下条件通过:放置断点,您可以看到,可见性变为
GONE
,但在下一帧,它神奇地变成VISIBLE
。我试图理解这到底是怎么发生的。事实证明,该视图应用了一个动画,这在内部导致视图将其可见性更改为
VISIBLE
,直到完成动画:如果您进行调试,您将看到
myViewGroup
> 确实将其可见性更改为GONE
,但在下一帧,它会再次变得可见,以便运行动画。因此,如果遇到这种情况,请确保在动画视图过程中没有执行
if
检查。您可以通过
View.clearAnimation 删除视图上的所有动画()
。Today I had a scenario, where I was performing following:
Right on the next frame I was performing an
if
check somewhere else for visibility state of that view. Guess what? The following condition was passing:Placing breakpoints you can see, that visibility changes to
GONE
, but right on the next frame it magically becomesVISIBLE
. I was trying to understand how the hell this could happen.Turns out there was an animation applied to this view, which internally caused the view to change it's visibility to
VISIBLE
until finishing the animation:If you debug, you'll see that
myViewGroup
indeed changes its visibility toGONE
, but right on the next frame it would again become visible in order to run the animation.So, if you come across with such a situation, make sure you are not performing an
if
check in amidst of animating the view.You can remove all animations on the view via
View.clearAnimation()
.你可以把它看作是CSS样式的可见性和可见性。展示。
You can think it as a CSS style visibility & display.
首先查看您的代码:
在这里您将两个可见性设置为同一字段,这就是问题所在。
我为该示例演示提供了一个示例
First see your code:
Here you set both visibility to same field so that's the problem.
I give one sample for that sample demo
就我而言,我发现只需在将可见性设置为“GONE”之前清除视图上的动画即可。
我遇到了类似的问题,我在两个视图之间切换,其中一个视图必须始终以
GONE
开头 - 但是当我再次显示视图时,即使setVisibility,它也会显示在第一个视图上(GONE)
被调用。在将视图设置为“GONE”之前清除动画是有效的。In my case I found that simply clearing the animation on the view before setting the visibility to
GONE
works.I had a similar issue where I toggle between two views, one of which must always start off as
GONE
- But when I displayed the views again, it was displaying over the first view even ifsetVisibility(GONE)
was called. Clearing the animation before setting the view toGONE
worked.View.GONE 该视图是不可见的,并且不占用任何布局空间。
View.INVISIBLE 该视图是不可见的,但出于布局目的,它仍然占用空间。
View.GONE This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE This view is invisible, but it still takes up space for layout purposes.
这是写给尝试了所有答案但仍然失败的人的。扩展pierre的答案。如果您使用动画,则将可见性设置为
GONE
或INVISIBLE
或invalidate()
永远不会起作用。尝试下面的解决方案。`
`
This is for someone who tried all the answers and still failed. Extending pierre's answer. If you are using animation, setting up the visibility to
GONE
orINVISIBLE
orinvalidate()
will never work. Try out the below solution.`
`
View.GONE 使视图不可见,而不占用视图在布局中的空间。
View.INVISIBLE 使视图不可见,但仍然占用空间。
您首先在同一个视图上使用 GONE,然后使用 INVISIBLE。因为代码是按顺序执行的,所以视图首先变为 GONE,然后它被仍然占用空间的 INVISIBLE 类型覆盖。
您应该在按钮上添加按钮侦听器,并在 onClick() 方法内部使视图可见。根据我的说法,这应该是您的 onCreate() 方法中的逻辑。
我认为这应该很容易实现。希望这有帮助。
View.GONE makes the view invisible without the view taking up space in the layout.
View.INVISIBLE makes the view just invisible still taking up space.
You are first using GONE and then INVISIBLE on the same view.Since, the code is executed sequentially, first the view becomes GONE then it is overridden by the INVISIBLE type still taking up space.
You should add button listener on the button and inside the onClick() method make the views visible. This should be the logic according to me in your onCreate() method.
I think this should work easily. Hope this helps.
我也面临着同样的问题,如果假设特定片段在不同的屏幕上膨胀,则 if 语句内设置的可见性模式有可能无法根据我们的需要发挥作用,因为条件可能在膨胀时被重置在我们的应用程序中多次。
就我而言,我必须根据在另一个片段(父片段)中单击的按钮来更改一个片段(子片段)中的可见性模式。因此,我将 buttonClicked 布尔值存储在父片段的变量中,并将其作为参数传递给子片段中的函数。因此,子片段中的可见性模式会根据通过参数接收的布尔值进行更改。但随着这个子片段在各个屏幕上膨胀,即使我使用 View.GONE 将其隐藏,可见性模式也会不断重置。
为了避免这种冲突,我在子片段中声明了一个静态布尔变量,每当从父片段接收到该布尔值时,我都会将其存储在静态变量中,然后根据子片段中的该静态变量更改可见性模式。
这为我解决了这个问题。
I was also facing the same issue, if suppose that particular fragment is inflated across various screens, there is a chance that the visibility modes set inside the if statements to not function according to our needs as the condition might have been reset when it is inflated a number of times in our app.
In my case, I have to change the visibility mode in one fragment(child fragment) based on a button clicked in another fragment(parent fragment). So I stored the buttonClicked boolean value in a variable of parent fragment and passed it as a parameter to a function in the child fragment. So the visibility modes in the child fragments is changed based on that boolean value that is received via parameter. But as this child fragment is inflated across various screens, the visibility modes kept on resetting even if I make it hidden using View.GONE.
In order to avoid this conflict, I declared a static boolean variable in the child fragment and whenever that boolean value is received from the parent fragment I stored it in the static variable and then changed the visibility modes based on that static variable in the child fragment.
That solved the issue for me.
我曾经遇到过同样的问题。更改可见性后调用 invalidate 几乎总是有效,但在某些情况下却无效。我不得不作弊并将背景设置为透明图像并将文本设置为“”。
I had the same problem once. Calling invalidate after changing visibility almost always works, but in some cases it doesn't. I had to cheat and set the background to a transparent image and set the text to "".
因为您将可见性设置为 true 或 false。
尝试一下
setVisible(0)
为可见 true 。和 setVisible(4)
为可见 false。Because you set visibility either true or false.
try that
setVisible(0)
to visible true .and setVisible(4)
to visible false.