如何同时隐藏多个视图?

6
我有一个RelativeLayout视图,还有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);
    }

1
请发布您的布局和代码。您所描述的方式是正确的,通常情况下隐藏布局会隐藏所有子元素。 - user658042
这真的很奇怪,我想知道是否与相对布局有关... 您可以尝试将其临时更改为线性布局,看看是否会出现相同的问题。 - Tolga E
请问您能否将整个活动代码粘贴在这里? - Pawan
解决了。在我的 Android 设备上卸载并重新安装应用程序就解决了问题。以后我会注意这点。 - Matt Parkins
4个回答

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

或者

您可以尝试:

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
好的,我可以这样做,但如果对父级进行操作就足够了,那么这将是很多冗余代码。我将在周一再次查看并使用上述建议。 - Matt Parkins

2

你必须将其设置为View.GONE状态。


2
他可能只是想隐藏(不可见)它...但值得一试。 - serkanozel
1
消失似乎并没有帮助 - 它只是将剩下可见的两个子元素向屏幕上方移动了一点。 - Matt Parkins

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


有趣的是,上面的代码是来自Eclipse ADT插件的自动生成代码。我会尝试一下并回复你。 - Matt Parkins
我已经研究过这个问题,"+"符号并没有太大的影响——它更多是用于检查您是否引入了拼写错误,但不会影响输出结果。 - Matt Parkins

-4
解决了。在我的安卓设备上卸载然后重新安装该应用程序就解决了问题。我将来会注意这一点。

这对我来说是解决方案。 - Matt Parkins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接