Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在onClick上切换布局的可见性_Java_Android_Layout - Fatal编程技术网

Java 在onClick上切换布局的可见性

Java 在onClick上切换布局的可见性,java,android,layout,Java,Android,Layout,我需要在单击按钮时将此布局设置为可见,我的java如下所示: Layout propLayout = (Layout) findViewById(R.id.properLayout); public void propsBtn(View view) { propLayout.setVisiblity(View.Visible); } 我知道我完全弄错了布局线!如果有人能告诉我如何正确设置,我将非常感激:) 这是我的XML: <FrameLayout xmlns:android=

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

目录
目录
试试这个:

int counter = 0;
    TextVeiw tv = (TextView) findViewById(R.id.textView);
Button button = (Button ) findViewById(R.id.but);
button.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
    // TODO Auto-generated method stub
    counter++;
    if(counter%2==0){
        tv.setVisibility(View.Visible);//show
    } else {
        tv.setVisibility(View.Gone);//hide
    }
}
});

您需要在要切换的布局上设置一个
OnClickListener()
。然后在侦听器内部检查布局是否可见。如果是,则将其可见性设置为
View.INVISIBLE
。否则,您将其设置为查看。可见

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

另一种切换方式,如果有人喜欢,可以减少线条

// button to click
ImageButton properties = (ImageButton) findViewById(R.id.propBtn);

// and LinearLayout to toggle
final LinearLayout propLayout = (LinearLayout) findViewById(R.id.properLayout);

properties.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        propLayout.setVisibility((propLayout.getVisibility() == View.VISIBLE) 
          ? View.INVISIBLE 
          : View.VISIBLE);
    }
});

他想切换布局的可见性。这将仅在每次单击
按钮时将其设置为可见。感谢您的回答:)但是。。我正在尝试设置布局可见性,而不是文本视图。这就是我的问题,我不知道如何在Java中编程布局定义行,只需将视图替换为文本视图任何从视图继承的东西都将被切换请粘贴布局的xml代码