【问题标题】:Set RelativeLayout visibility to GONE does not affect children将 RelativeLayout 可见性设置为 GONE 不会影响子级
【发布时间】:2013-08-16 01:05:22
【问题描述】:

我有一个RelativeLayout,叫它my_rel,有很多孩子。我使用include 标签将my_rel 包含到另一个布局中。无论如何,当我将my_rel 的可见性设置为 GONE 时,my_rel 的孩子仍然可见?如何防止这种情况发生?

这是大图/信封视图

<RelativeLayout 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="#F0F1F3"
    tools:context=".CookingFiest" >

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="84dp"
        android:background="#CCCCCC" />

    <View
        android:layout_width="4dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="85dp"
        android:background="#FFFFFFFF" />

    <View
        android:layout_width="1dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="89dp"
        android:background="#CCCCCC" />

    <ScrollView
        android:id="@+id/my_scroller"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dip"
        android:isScrollContainer="false" >

        <LinearLayout
            android:id="@+id/timeline_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <com.nothing.noevil.widget.HorizontalListView
        android:id="@+id/cool_stuff"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_above="@id/cool_stuff"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp" />

    <include
        android:id="@+id/appliance"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/kitchen" />

    <RelativeLayout
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="10dp"
        android:background="#00000000"
        android:clickable="true" >

        <ImageView
            android:id="@+id/cook"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:clickable="true"
            android:onClick="oncookClicked"
            android:src="@drawable/match_here" />

        <ImageView
            android:id="@+id/shoe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/cook"
            android:clickable="true"
            android:onClick="onshoeClicked"
            android:src="@drawable/shoe" />

        <ImageView
            android:id="@+id/pencil"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/cook"
            android:clickable="true"
            android:onClick="onRiverClicked"
            android:src="@drawable/pencil" />
    </RelativeLayout>

</RelativeLayout>

这是嵌套/包含的视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="40dp"
    android:clickable="true"
    android:visibility="gone"
    android:onClick="mangoes" >

    <!-- Backgrounds -->

    <ImageView
        android:id="@+id/bkg_1"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentTop="true"
        android:background="#e8edf3" />

    <ImageView
        android:id="@+id/bkg_2"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:background="#f6f6f6" />

    <!-- Two timers -->

    <TextView
        android:id="@+id/orange"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/bkg_1"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@id/bkg_1"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="2dp"
        android:gravity="center_vertical"
        android:text=""
        android:textColor="#1083f0"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/apple"
        android:layout_width="65dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/bkg_2"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@id/bkg_2"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:text=""
        android:textColor="#555555"
        android:textSize="12sp"
        android:textStyle="bold" />
    <!-- Spot icon -->

    <ImageView
        android:id="@+id/veggies"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/bkg_2"
        android:layout_alignTop="@id/bkg_1"
        android:layout_marginBottom="2dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/orange"
        android:background="#000000"
        android:padding="10dp"
        android:src="@drawable/img1" />
    <!-- Descriptions -->

    <TextView
        android:id="@+id/sheo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/bkg_1"
        android:layout_alignTop="@id/bkg_1"
        android:layout_marginBottom="3dp"
        android:layout_toRightOf="@id/veggies"
        android:gravity="center_vertical"
        android:text=""
        android:textColor="#1083f0"
        android:textSize="12sp" />

    <TextView
        android:id="@+id/chalks"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/bkg_2"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/bkg_2"
        android:layout_marginTop="3dp"
        android:layout_toRightOf="@id/veggies"
        android:gravity="center_vertical"
        android:text=""
        android:textColor="#555555"
        android:textSize="12sp"
        android:textStyle="bold" />

</RelativeLayout>

如果我在 xml 中设置 GONE 就可以了。但如果我以编程方式设置消失,它会被忽略。

LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        kich = (RelativeLayout) mInflater.inflate(R.layout.kitchen, null);
        kich.setVisibility(View.GONE);

【问题讨论】:

  • 您应该包含您的xml 文件。
  • 这没有意义。如果包含正确生成包含的视图作为子视图,它应该按预期工作。请发布代码以支持您的声明。
  • 包括您的布局 XML。
  • 我还添加了代码部分
  • 您是否已将膨胀视图 kich 附加到其他视图(如内容视图)?

标签: android android-layout android-relativelayout


【解决方案1】:

当你像你一样将include 膨胀到你的 main_view 中时,这很奇怪。

试试这个,在 R.layout.kitchen xml 里面:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/kitchen_parent" <!-- HERE -->
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="40dp"
    android:clickable="true"
    android:onClick="mangoes"
    android:visibility="gone" > 

在你的Activity

ViewStub stub = (ViewStub) findViewById(R.id.appliance);
stub.inflate();
RelativeLayout kitchen_parent = findViewById(R.id.kitchen_parent);
kitchen_parent.setVisibility(View.GONE);

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我通过实例化来解决问题

    kich = (RelativeLayout) findViewById(R.id.appliance)
    

    id在include标签中

      <include
        android:id="@+id/appliance"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        layout="@layout/kitchen" />
    

    【讨论】:

    • 好点!我在定义 View Kich = findViewById(R.id.appliance).. 时遇到了问题,因此它不会照顾孩子,而是将其视为视图。转换为 RelativeLayout 是关键。可能同样适用于其他布局!
    • 现在说这个还为时过早,我错了。这个解决方案仍然没有奏效。孩子们仍然在表演。
    猜你喜欢
    • 2011-11-03
    • 1970-01-01
    • 2015-05-26
    • 2022-10-02
    • 2015-08-30
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多