2014-12-22 60 views
0

我正在嘗試使視圖完全覆蓋另一個視圖以「灰顯」。所以我用一個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既包括文本1和文本。

+0

你可以發佈您的包含的佈局呢?因爲我可以在沒有它們的情況下運行這個代碼。 –

+0

爲什麼在你的視圖中有一個android:orientation =「vertical」? – TheRedFox

+0

@FarukYazıcı增加了一個更容易測試的例子。 – schoel

回答

2

這裏是一個答案,在第二個例子中你的問題;

  1. TextView S IN的佈局與ID
  2. 調整你View的對齊參數如下:

    android:layout_alignLeft="@+id/texts" 
    android:layout_alignRight="@+id/texts" 
    android:layout_alignTop="@+id/texts" 
    android:layout_alignBottom="@+id/texts" 
    

通過這樣做,你正迫使該視圖留在其他佈局的邊界。 因此,可測試示例的完整代碼將會是;

<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> 

剛剛嘗試這一點,你會看到它僅覆蓋了文本:)

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> 

然後把這個地方變成當要灰出或ungrey時的觀點你的觀點是正確的,現在:-)的地方,簡單地改變covering_view觀的visibility :-)

至於你的例如,請嘗試更改覆蓋觀點如下:

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

RelativeLayout是列表中的條目。我想覆蓋視圖以涵蓋條目(即整個RelativeLayout命名爲unit_entry) – schoel

+0

更新了答案 – Kelevandos

+0

match_parent和fill_parent都使視圖佔用0高度。 – schoel