Android 条件显示按钮

Android 条件显示按钮,android,android-layout,Android,Android Layout,我知道如何在找到布局XML中定义的按钮后启用/禁用该按钮: testBtn = (Button)findViewById(R.id.test); 但是除了有条件地加载布局之外,还有没有其他方法可以在我的代码中告诉我“使用布局XML,但不要加载在那里定义的按钮”要在XML中设置视图的可见性,请使用android:visibility属性 以下内容将按钮可见性设置为“消失”。当设置为gone时,Android将不显示按钮,并且在布局计算期间不包括其大小 <Button android:i

我知道如何在找到布局XML中定义的按钮后启用/禁用该按钮:

  testBtn = (Button)findViewById(R.id.test);

但是除了有条件地加载布局之外,还有没有其他方法可以在我的代码中告诉我“使用布局XML,但不要加载在那里定义的按钮”

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

以下内容将按钮可见性设置为“消失”。当设置为gone时,Android将不显示按钮,并且在布局计算期间不包括其大小

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

您的假设是正确的-我不希望它作为布局的一部分呈现。我想在代码中动态地控制它,API方法是什么?非常感谢。你刚刚在Android世界救了一个蹒跚学步的孩子的命。:)
Button btn = (Button)findViewById(R.id.thebuttonid);
btn.setVisibility(View.VISIBLE); //View.GONE, View.INVISIBLE are available too.