2017-07-27 17 views
0

我嘗試在RelativeLayout中創建一個半透明的紅色視圖和一個TextView,其中View的高度遵循TextView的高度,當TextView位於上方時它按預期工作半透明紅色景觀:android:layout_alignTop不起作用類似於android:layout_alignBottom

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<View 
    android:background="#FF0000" 
    android:alpha="0.5" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@id/textView"/> 
<TextView 
    android:id="@+id/textView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="TEST" 
    android:textSize="50dp" 
    android:textColor="#000000"/> 
</RelativeLayout> 

enter image description here

,但我想改變它了一下:把視圖TextView的上述和其他利率維持不變,我試圖改變android:layout_alignBottomandroid:layout_alignTop

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<View 
    android:background="#FF0000" 
    android:alpha="0.5" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@id/textView"/> 
<TextView 
    android:id="@+id/textView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="TEST" 
    android:textSize="50dp" 
    android:textColor="#000000"/> 
</RelativeLayout> 

但現在看來View不遵循TextView高度:

enter image description here

這是什麼問題,我該如何解決?

+0

我認爲這是不可能由xml完成​​的。如果您需要將'View'放在'TextView'上方,則無法實現其高度對齊。 – BakaWaii

回答

0

在視圖中添加android:layout_alignBottom="@+id/textView"。 EG-

<View 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@id/textView" 
     android:layout_alignBottom="@+id/textView" 
     android:alpha="0.5" 
     android:background="#FF0000" /> 
0

我想改變它一下:把查看TextView的以上和其它不變

如果說上述你的意思是上面的TextView關於z軸,那麼你的方式是錯誤的。

你可以做什麼,只是改變佈局中視圖的排列順序:聲明爲第一個的佈局將首先佈局,下一個將佈置在它的頂部。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="TEST" 
     android:textSize="50dp" 
     android:textColor="#000000"/> 

    <View 
     android:background="#FF0000" 
     android:alpha="0.5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView"/> 
</RelativeLayout>