我需要在单击按钮时将此布局设置为可见,我的 java 是这样的:
Layout propLayout = (Layout) findViewById(R.id.properLayout);
public void propsBtn(View view) {
propLayout.setVisiblity(View.Visible);
}
我知道我对布局线完全错了!如果有人能告诉我如何正确设置它,我将不胜感激:)
这是我的 XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/mainBackGround"
tools:context="com.myapplication2.app.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="@+id/properLayout">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="bottom"
android:padding="8dp">
...contents...
</RelativeLayout>
</LinearLayout>
最佳答案
DerGolem 给出了正确的答案,但他随后将其删除,所以我现在在这里报告:
//First we set visibility value of interested layout either to gone, visible or invisible
android:visibility="gone"
然后在onCreate下这样写:
//specify the button that has to handle visibility
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);
//And the layout we want to change is visibility
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);
properties.setOnClickListener
(
new View.OnClickListener()
{
public void onClick(View v)
{
if (propLayout.getVisibility() == View.VISIBLE)
{
propLayout.setVisibility(View.INVISIBLE);
}
else
{
propLayout.setVisibility(View.VISIBLE);
}
}
}
);
关于java - 在 onClick 上切换布局的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23722762/