0

我想将两个LinearLayouts放在RelativeLayout(rootelement)中。我只能看到一行。 ÄÄroidroid:layout_below **不应该强制第二个Linearlayout放置在第一个LinearLayout之下吗?在相对布局中换行linearlayout

出了什么问题?是否因为LinearLayout本身就是一个ViewGroup?

<?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="match_parent"> 

<LinearLayout 
    android:id="@+id/addon1_row" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/weapon1_id" 
     android:layout_width="100dp" 
     android:layout_height="26dp" 
     android:src="@mipmap/weapon1" 
     android:adjustViewBounds="true" 
     /> 

    <ImageView 
     android:layout_width="100dp" 
     android:layout_height="26dp" 
     android:src="@mipmap/buy_button" 
     android:adjustViewBounds="true" 
     /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/addon2_row" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:layout_below="@+id/addon1_row"> 

    <ImageView 
     android:id="@+id/weapon2_id" 
     android:layout_width="100dp" 
     android:layout_height="26dp" 
     android:src="@mipmap/weapon2" 
     android:adjustViewBounds="true" 
     /> 

    <ImageView 
     android:layout_width="100dp" 
     android:layout_height="26dp" 
     android:src="@mipmap/buy_button" 
     android:adjustViewBounds="true" 

     /> 
</LinearLayout> 


</RelativeLayout> 
+1

你并不真的需要一个RelativeLayout的,鉴于你目前的安排。但是我会通过使用单个RelativeLayout而不使用LinearLayouts或通过使用GridView(再次没有额外的LinearLayouts)来优化它(为了性能)。 –

+1

@Rotwang,谢谢我会考虑这一点 – java

回答

1

这是因为您设置了线性布局的高度来"match_parent"只是将其设置为"wrap_content"

<LinearLayout 
    android:id="@+id/addon1_row" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" <== Here 
    android:orientation="horizontal"> 


<LinearLayout 
    android:id="@+id/addon2_row" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" <== Here 
    android:layout_below="@+id/addon1_row" 
    android:orientation="horizontal">