在onClick上切换布局的可见性 [英] Toggle visibility of a layout on onClick

查看:53
本文介绍了在onClick上切换布局的可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮时,我需要将此布局设置为可见,我的java就像这样:

I need to set this layout visible when i click a button, my java is like:

Layout propLayout = (Layout) findViewById(R.id.properLayout);

    public void propsBtn(View view) {
propLayout.setVisiblity(View.Visible);
}

我知道我对版式行完全错了!如果有人可以告诉我如何设置正确的位置,我将不胜感激:)

I know I'm totally wrong with the layout line! I'll be very grateful if someone could show me how to set it right :)

这是我的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给出了正确答案,但他随后将其删除,所以我现在在这里报告:

DerGolem gave the right answer but he then deleted it, so I report it here now:

//First we set visibility value of interested layout either to gone, visible or invisible
android:visibility="gone"

然后在onCreate下这样写:

Then under onCreate write like:

//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);
                            }
                        }
                    }
            );

这篇关于在onClick上切换布局的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆