2016-12-06 5 views
2

Ich habe versucht, zwei ImageView vertikal in einem RelativeLayout anzuzeigen, jedes ImageView hat 50% als Gewicht, aber die Anzeige nur ein ImageView wird angezeigt.Warum layout_weight funktioniert nicht korrekt in RelativeLayout?

Code:

<RelativeLayout 
    android:id="@+id/layout_board" 
    android:layout_width="match_parent" 
    android:layout_height="450dp" 
    android:background="@color/colorPrimaryDark" 
    android:weightSum="2"> 

<ImageView android:id="@+id/imageView11" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@color/colorAccent" 
      android:adjustViewBounds="true" 
      android:scaleType="fitXY"/> 

<ImageView android:id="@+id/imageView21" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@color/colorAccent" 
      android:adjustViewBounds="true" 
      android:scaleType="fitXY"/> 



</RelativeLayout> 

Bitte um Hilfe!

+5

'layout_weight' funktioniert nur mit' LinearLayout'. –

Antwort

0

einfach, Ändern der Wurzel RelativeLayout zu LinearLayout Ihr Problem lösen und layout_weight Attribut in Ihrem Code verwenden Sie habenLinearlayout zu verwenden, anstatt die RelativeLayout der Verwendung als Viewgroup. Es gibt keine layout_weight Attribut in RelativeLayout

1

android:layout_weight ist kein Attribut von RelativeLayout es ein Attribut LinearLayout ist. So können Sie das übergeordnete Layout LinearLayout ändern oder können Sie PercentRelativeLayout

Code-Schnipsel

<android.support.percent.PercentRelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    <ImageView 
     app:layout_widthPercent="50%" 
     app:layout_heightPercent="50%" 
     app:layout_marginTopPercent="25%" 
     app:layout_marginLeftPercent="25%"/> 
</android.support.percent.PercentRelativeLayout> 
0

Linearlayout unter RelativeLayout hinzufügen verwenden. WeightSum ist das Attribut von LinearLayout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 


     <LinearLayout 
      android:id="@+id/layout_board" 
      android:layout_width="match_parent" 
      android:layout_height="450dp" 
      android:orientation="horizontal" 
      android:background="@color/colorPrimaryDark" 
      android:weightSum="2"> 

      <ImageView 
       android:id="@+id/imageView11" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:background="@color/colorAccent" 
       android:scaleType="fitXY" /> 

      <ImageView 
       android:id="@+id/imageView21" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:adjustViewBounds="true" 
       android:background="@color/colorAccent" 
       android:scaleType="fitXY" /> 
     </LinearLayout> 
    </RelativeLayout> 
Verwandte Themen