RelativeLayout子控件实现LinearLayout的权重效果

本篇记录 RelativeLayout 中实现权重效果

先上效果图

然后上布局文件,根据代码来说

<?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="120dp"
    android:background="@drawable/style_alert_dialog_addplayer">

    <EditText
        android:id="@+id/et_addPlayerName"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="50dp"
        android:background="@drawable/style_edittext_round_bg"
        android:inputType="text" />
    <Button
        android:id="@+id/btn_addPlayer"
        android:layout_width="50dp"
        android:layout_height="120dp"
        android:layout_alignParentEnd="true"
        android:background="@null"
        android:text="@string/add"
        android:textColor="@color/colorAccent" />

</RelativeLayout>

复制代码

在布局文件中,我们先设置了外层的 RelativeLayout 的高宽,通过 Button 来撑起他的高度。

然后 Button 通过和父布局的右边对齐 android:layout_alignParentEnd=”true”

然后在要实现权重的控件 EditText 上设置 android:layout_width=“fill_parent” 就可以了

简单记录下。

参考了这篇文章
RelativeLayout 的自适应

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享