你如何一次隐藏多个视图?

时间:2011-11-18 16:30:20

标签: android layout sdk visibility

我有一个相对布局视图,也有3个子视图。我试图通过使用setVisibility将相对布局设置为INVISIBLE来将它们全部隐藏在代码中。有趣的是,当我使用setVisibility(View.INIVISIBLE)时,只隐藏第一个子节点而不是其他两个子节点。所以我有点困惑 - 如果我将父视图设置为隐形不应该改变所有孩子的可见性或者让他们独自一人?

随意给我一个解释它的参考页面 - 我找不到任何东西。

更新:我已经尝试将其设置为View.GONE,但同样的事情发生了,除了两个仍然可见的孩子向上移动了一点。

以下是相关的XML:

<RelativeLayout
    android:id="@+id/optionsform"
    android:layout_width="fill_parent"
    android:padding="8dp"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvoptions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/tvoptions"
        android:textColor="#f000"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="bold"/>

    <TextView
        android:id="@+id/tvdictionary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tvoptions"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="16dp"
        android:text="@string/dictionary"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#f000" />

    <Spinner
        android:id="@+id/dictionary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tvdictionary"
        android:layout_alignParentRight="true"
        android:layout_marginTop="-10dp"
        android:layout_marginLeft="6dp"
        android:layout_toRightOf="@+id/tvdictionary" />

</RelativeLayout>

这是我正在使用的相关代码:

    public void onClick(View v) {
        //Toggle viewing of options, using "if" in case it is set to View.GONE 
        View view = findViewById(R.id.optionsform);
        if (view.getVisibility() == View.VISIBLE) 
            view.setVisibility(View.INVISIBLE);
        else
            view.setVisibility(View.VISIBLE);
    }

4 个答案:

答案 0 :(得分:3)

尝试将所有三个视图设置为View.INVISIBLEView.GONE

OR

你可以尝试

public void onClick(View v) {
    //Toggle viewing of options, using "if" in case it is set to View.GONE 
    RelativeLayout view = (RelativeLayout) findViewById(R.id.optionsform);
    if (view.getVisibility() == View.VISIBLE) 
        view.setVisibility(View.INVISIBLE);
    else
        view.setVisibility(View.VISIBLE);
}

答案 1 :(得分:2)

您必须设置为View.GONE州。

答案 2 :(得分:1)

 <TextView
        android:id="@+id/tvdictionary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        **android:layout_below="@+id/tvoptions"** // *should  be  android:layout_below="@id/tvoptions*
        android:layout_marginLeft="30dp"
        android:layout_marginTop="16dp"
        android:text="@string/dictionary"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#f000" />



     <Spinner
            android:id="@+id/dictionary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            **android:layout_alignTop="@+id/tvdictionary"** // *should be android:layout_alignTop="@id/tvdictionary*
            android:layout_alignParentRight="true"
            android:layout_marginTop="-10dp"
            android:layout_marginLeft="6dp"
            android:layout_toRightOf="@+id/tvdictionary"// *should be android:layout_toRightOf="@id/tvdictionary*
 />
引用布局ID时使用

@id 创建新布局ID时使用@ + id

答案 3 :(得分:-2)

解决。卸载然后在我的Android设备上安装应用程序就可以了。我将来要小心这个

相关问题