将RelativeLayout Relative设置为GONE不会影响子项

时间:2013-08-15 06:20:41

标签: android android-layout relativelayout

我有一个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);

2 个答案:

答案 0 :(得分:5)

当您像往常一样将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);

希望这有帮助。

答案 1 :(得分:0)

我通过实例化为

来解决问题
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" />