查看完全覆盖另一个视图

时间:2014-12-22 07:20:57

标签: android

我正在尝试创建一个完全覆盖另一个视图的视图,以“将其灰显”。所以我添加了一个视图,在它上面有一个透明的颜色和RelativeLayout。但是,即使我使用“fill_parent”,顶部的视图也占据0高度。如果我改为,例如,TextView,它占用的空间就像wrap_content一样多。如何在顶部覆盖整个视图(尺寸可能不同)? 我的真实代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/unit_entry"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="3dip">

    <include layout="@layout/view_stats"
        android:id="@+id/unit_stats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/unit_stats">

        <LinearLayout 
            android:layout_weight="1"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <include layout="@layout/view_heading_value_text"
                android:id="@+id/unit_exp_layout"/>

            <TextView 
                android:id="@+id/unit_skills_text"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:hint="@string/skills_injuries_hint"
                android:background="@drawable/border"/>

        </LinearLayout>

        <TextView 
            android:id="@+id/unit_equipment_text"
            android:layout_weight="1"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:hint="@string/equipment_hint"
            android:background="@drawable/border"/>

    </LinearLayout>

    <View android:id="@+id/unit_overlay"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:background="@color/gray_overlay"
        />
</RelativeLayout> 

说明我想要的例子:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/unit_entry"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="3dip">


    <TextView 
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="text1"/>

    <TextView 
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/text1"
        android:text="text2"/>

    <View android:id="@+id/unit_overlay"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:background="#88666666"
        />
</RelativeLayout>

在这个例子中,我希望unit_overlay涵盖text1和text2。

2 个答案:

答案 0 :(得分:2)

以下是关于第二个例子的问题的答案;

  1. TextView放入ID为
  2. 的布局中
  3. 按如下方式调整View的对齐参数;

    android:layout_alignLeft="@+id/texts"
    android:layout_alignRight="@+id/texts"
    android:layout_alignTop="@+id/texts"
    android:layout_alignBottom="@+id/texts"
    
  4. 通过执行此操作,您将强制该视图保持在其他布局的边界内。 因此,您的可测试示例的完整代码将是;

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/unit_entry"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingBottom="3dip" >
    
        <LinearLayout
            android:id="@+id/texts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/text1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="text1" />
    
            <TextView
                android:id="@+id/text2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/text1"
                android:text="text2" />
        </LinearLayout>
    
        <View
            android:id="@+id/unit_overlay"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/texts"
            android:layout_alignRight="@+id/texts"
            android:layout_alignTop="@+id/texts"
            android:layout_alignBottom="@+id/texts"
            android:background="#88666666" />
    
    </RelativeLayout>
    

    试试这个,你会看到它只包含文字:)

答案 1 :(得分:0)

我不确定您的代码中的哪些视图应该是应该涵盖的视图,但通常您想要的是这样的:

首先,将要覆盖的视图放在单独的RelativeLayout中:

<RelativeLayout
    android:layout_height = "wrap_content"
    android:layout_width = "wrap_content" >

    <View
        android:layout_height = "@+id/covering_view"
        android:background = "@color/ /*the grey-out color here */"
        android:layout_height = "match_parent"
        android:layout_width = "match_parent"  />

    /* Your view which you want to cover */

</RelativeLayout>

然后将其放入您的视图现在的位置:-)如果您想要显示视图,只需更改visibility视图的covering_view: - )

至于您的示例,请尝试更改叠加视图,如下所示:

<View android:id="@+id/unit_overlay"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@color/gray_overlay"
    />
相关问题