有条件的显示按钮 [英] Conditionally displaying button
问题描述
我知道如何启用/找到它后禁止在布局XML定义的按钮:
I know how to enable/disable a button defined in a layout XML after finding it:
testBtn = (Button)findViewById(R.id.test);
但是,除了有条件地装载布局等,有没有办法在我的code告诉使用该布局XML,但不加载其中定义的按钮?
But other than conditionally loading layouts, is there a way to tell in my code "Use that layout XML, but don't load the button defined there"?
推荐答案
在XML中设置视图的可见性,使用android:visibility属性。
To set a View's visibility in Xml, use the android:visibility attribute.
下面的设置按钮能见度走了。当设置为离去的Android不会显示该按钮,而不是布局计算时包括它的大小。
The following sets the button visibility to gone. When set to gone Android will not show the button and not include it's size during layout calculation.
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:visibility="gone"/>
设置机器人:知名度=看不见不会显示按钮,但布局计算中包含它
Setting android:visibility="invisible" will not show the button, but include it during layout calculation.
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:visibility="invisible"/>
要编程显示$ C $的按钮C调用setVisibility()方法。
To programmatically show the button in code you call the setVisibility() method.
Button btn = (Button)findViewById(R.id.thebuttonid);
btn.setVisibility(View.VISIBLE); //View.GONE, View.INVISIBLE are available too.
这篇关于有条件的显示按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!