2017-07-31 38 views
0

在HTML中,如果我想用一个蓝色覆盖到完全覆盖一个黄色横幅,我可以做到这一点:在相同的父级布局下,如何使match_parent元素的大小跟随wrap_content元素?

<div style="position:relative;display:table;"> 
 
    <div style="background-color:yellow;font-size:30px;">TESTING<div/> 
 
    <div style="z-index:1;position:absolute;top:0px;left:0px;width:100%;height:100%;background-color:blue;opacity:0.5;display:inline-block;"></div> 
 
</div>

其中蓝色覆盖物的大小可以按照黄色旗帜,尽管我不知道它的实际px。

我想实现在它的Android一样的效果,我想:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
<TextView 
    android:background="#FFFF00" 
    android:textSize="30dp" 
    android:id="@+id/textview" 
    android:text="TESTING" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
<TextView 
    android:alpha="0.5" 
    android:background="#0000FF" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignBottom="@+id/textview" 
</RelativeLayout> 

但蓝色覆盖的宽度不遵循即使我设置的Android封闭WRAP_CONTENT元素:layout_alignBottom:

enter image description here

我该如何获得与html相同的效果?

回答

1

如果我正确理解你需要的是,那么你肯定是在寻找android:layout_alignEnd

根据文档

android:layout_alignEnd:使该视图的端部边缘匹配给定锚视图ID的端部边缘。

所以,你可以在你的第二个textView

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

<TextView 
    android:id="@+id/textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#FFFF00" 
    android:text="TESTINGsd" 
    android:textSize="30dp" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignBottom="@+id/textview" 
    android:layout_alignEnd="@+id/textview" 
    android:alpha="0.5" 
    android:background="#0000FF"/> 

输出使用它:

enter image description here

0

使用布局,

    任何布局
  • 机器人:layout_width = “WRAP_CONTENT”

  • 机器人:layout_height = “WRAP_CONTENT”

属性。然后添加一个背景

  • 机器人:背景= “#0000FF”

并把TextView的它里面。

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

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#0000FF" 
     > 

     <TextView 
      android:background="#FFFF00" 
      android:textSize="30dp" 
      android:id="@+id/textview" 
      android:text="TESTING" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 
</RelativeLayout> 
相关问题