2011-03-30 58 views
3

我知道如何启用/发现它后禁用布局XML定义的按钮:有条件地显示按钮

testBtn = (Button)findViewById(R.id.test); 
比有条件装载布局

但是其他的,有没有办法在我的代码“使用告诉布局XML,但不加载在那里定义的按钮“?

回答

11

要在Xml中设置视图的可见性,请使用android:visibility属性。

以下设置按钮可见性消失。当设置为消失时,Android不会显示该按钮,并且在布局计算过程中不包括它的大小。

<Button android:id="@+id/mybutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hello" 
      android:visibility="gone"/> 

设置android:visibility =“不可见”不会显示按钮,但会在布局计算中包含它。

<Button android:id="@+id/mybutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Hello" 
      android:visibility="invisible"/> 

要以编程方式显示代码中的按钮,请调用setVisibility()方法。

Button btn = (Button)findViewById(R.id.thebuttonid); 
btn.setVisibility(View.VISIBLE); //View.GONE, View.INVISIBLE are available too. 
+0

您的假设是正确的 - 我不希望它呈现为布局的一部分。我想在代码中动态地控制它,API方法是做什么的? – an00b 2011-03-30 16:46:47

+1

非常感谢。您刚刚在Android世界中拯救了幼儿的生命。 :) – an00b 2011-03-30 16:56:57

+0

不客气。快乐编码=) – 2011-03-30 16:59:41