0

我有一个RelativeLayout。它里面我有:如何在其他布局之间放置布局在RelativeLayout中?

  • an ImageView 120x120 dp在右边。在左其他
  • 3个布局:
    • 1布局(称为顶部)具有alignParentTop=true
    • 第二个布局(称为底部)具有alignParentBottom=true
    • 第三布局(称为中东)是在中间(低于顶部及以上底部)。

的问题是:如果我的容器(RelativeLayout)设置layout_width="wrap_content",我没有看到中东布局。

如果我将它设置为一些值(例如:144dp),我会看到中间布局。

这里是布局结构(我隐藏了一些子布局,只显示主布局)。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="16dp"> 

    <LinearLayout 
     android:id="@+id/top" 
     android:background="#eeaaee" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/bottom" 
     android:background="#22eeaa" 
     android:layout_toLeftOf="@+id/image" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentBottom="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/middle" 
     android:layout_width="match_parent" 
     android:background="#44ee22" 
     android:layout_toLeftOf="@+id/image" 
     android:layout_height="64dp" 
     android:layout_below="@+id/top" 
     android:layout_above="@+id/bottom"> 
     <TextView 
      android:id="@+id/hotnews_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:textStyle="bold" 
      android:textSize="14sp" 
      /> 
    </LinearLayout> 

    <ImageView 
      android:id="@+id/image" 
      android:layout_alignParentEnd="true" 
      android:layout_width="120dp" 
      android:layout_height="120dp" 
      android:scaleType="centerCrop"/> 
</RelativeLayout> 
+0

也许我不明白你的问题,在你的中间布局中试试android:layout_alignParentLeft =“true”。希望能帮助到你。 –

+0

@AndreaEbano它不起作用。 – TOP

+0

你可以设置高度为match_parent –

回答

0

,如果你想要把比一些布局之间布局其更好地使用的LinearLayout作为所有三个(上,中,下)母公司layout.and使用它的方向和重量。我已经使用了linearlayout的固定高度来区分顶部,中部和底部视图。根据你的需要改变它。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:id="@+id/top" 
       android:layout_width="match_parent" 
       android:layout_height="40dp" 
       android:background="#eeaaee" 
       android:orientation="horizontal"></LinearLayout> 

      <LinearLayout 
       android:id="@+id/bottom" 
       android:layout_width="match_parent" 
       android:layout_height="40dp" 
       android:background="#22eeaa" 
       android:orientation="horizontal" /> 

      <LinearLayout 
       android:id="@+id/middle" 
       android:layout_width="match_parent" 
       android:layout_height="40dp" 
       android:background="#44ee22"> 

       <TextView 
        android:id="@+id/hotnews_title" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textSize="14sp" 
        android:textStyle="bold" /> 
      </LinearLayout> 


     </LinearLayout> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="120dp" 
      android:layout_height="120dp" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_weight="1" 
      android:scaleType="centerCrop" /> 


    </LinearLayout> 


</RelativeLayout> 
相关问题