java - 在 onClick 上切换布局的可见性

标签 java android layout

我需要在单击按钮时将此布局设置为可见,我的 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/

相关文章:

android onclick按钮在Socket上写入字符串

android - 在具有深度感知的 z 轴上旋转的 View 之间转换的动画

android - .xml 布局文件因 <EditText> 正文而无法正常工作

java正则表达式去除重复单词

使用 OC4J 10.1.3.5 和 ADF 关闭弹出表单时出现 java.lang.IllegalStateException

java - 使用 Apache Jena ExtendedIterator 在具有大量三元组的图上进行迭代

java - 日期转换为不同的语言环境

Android Studio 2.3 正在生成未对齐的签名 APK 而不是 zipaligned?

android - 我拍摄的照片没有出现在 ImageView 中

java - 如何检查列表中的列表是否具有相同的长度