an0*_*00b 3 android android-layout
我知道如何在找到布局XML后启用/禁用布局XML中定义的按钮:
testBtn = (Button)findViewById(R.id.test);
Run Code Online (Sandbox Code Playgroud)
但除了有条件地加载布局之外,有没有办法在我的代码中使用"使用该布局XML,但不加载那里定义的按钮"?
Rya*_*ves 12
要在Xml中设置View的可见性,请使用android:visibility属性.
以下设置按钮可见性消失.当设置为Android时,Android将不会显示按钮,并且在布局计算期间不会包含它的大小.
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:visibility="gone"/>
Run Code Online (Sandbox Code Playgroud)
设置android:visibility ="invisible"不会显示按钮,但在布局计算期间包含它.
<Button android:id="@+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:visibility="invisible"/>
Run Code Online (Sandbox Code Playgroud)
要以编程方式在代码中显示按钮,请调用setVisibility()方法.
Button btn = (Button)findViewById(R.id.thebuttonid);
btn.setVisibility(View.VISIBLE); //View.GONE, View.INVISIBLE are available too.
Run Code Online (Sandbox Code Playgroud)