2016-12-06 7 views
2

Я попытался отобразить два вертикальных изображения ImageView внутри RelativeLayout, каждый образ имеет 50% веса, но отображается только один ImageView.Почему layout_weight не работает должным образом внутри RelativeLayout?

Код:

<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> 

пожалуйста, помогите!

+5

'layout_weight' работает только с' LinearLayout'. –

ответ

0

Просто Изменение корня RelativeLayout для LinearLayout решить вашу проблему и использовать layout_weight атрибут в коде вы, чтобы использовать LinearLayout вместо использования RelativeLayout в ViewGroup. Там нет layout_weight атрибута в RelativeLayout

1

android:layout_weight не является атрибут RelativeLayout он является атрибутом LinearLayout. Таким образом, вы можете изменить родительский расположение в LinearLayout или вы можете использовать PercentRelativeLayout

фрагмент кода

<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 ниже RelativeLayout. weightSum - это атрибут 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> 
Смежные вопросы