2017-07-27 19 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>