android - 为什么 layout_weight 在 RelativeLayout 中不能正常工作?

标签 android android-layout-weight

我试图在一个 RelativeLayout 中垂直显示两个 ImageView,每个 imageView 有 50% 的权重,但显示只显示一个 ImageView。

代码:

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

请帮忙!

最佳答案

android:layout_weight 不是 RelativeLayout 的属性,它是 LinearLayout 的属性。因此,您可以将父布局更改为 LinearLayout 或者您可以使用 PercentRelativeLayout

代码 fragment

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

关于android - 为什么 layout_weight 在 RelativeLayout 中不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40986772/

相关文章:

java - Android 中出现 NullPOinterException 错误,请帮忙

android - android.os.Build.SERIAL 是唯一的吗?

android - 具有最大宽度和重量的水平按钮

android - 把Android布局按钮做成正方形

java - 连接丢失后 Firebase Firestore 需要很长时间才能重新连接

java - 弹出窗口显示位置,改变重力给出错误的位置

Android 2.3.3 闹钟源码

java - Android ListView : Fixed number of rows for EVERY screen size

android - 当在加权 LinearLayout 中显示时,RatingBar 被水平截断