【发布时间】:2012-01-01 09:46:27
【问题描述】:
我有一个RelativeLayout 视图,还有 3 个孩子的视图。我试图通过使用setVisibility 将相对布局设置为不可见来将它们全部隐藏在代码中。有趣的是,当我使用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);
}
【问题讨论】:
-
请发布您的布局和代码。您描述的方式是正确的,隐藏布局在正常情况下会隐藏所有子项。
-
这个真的很奇怪,不知道是不是和relative layout有关..能不能暂时改成linearlayout看看会不会出现同样的问题
-
你能把你的整个活动代码粘贴到这里吗..
-
已解决。在我的安卓设备上卸载然后安装应用程序就可以了。我以后会注意的。
标签: android layout sdk visibility