本文是看了鸿洋视频后的小结:

layout_height的作用:

首先按照声明的尺寸分配,剩余的空间再按照layout_weight进行分配

一平均分配:

layout_weight属性图解_比例


代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#e1d611"
        android:gravity="center"
        android:text="我是老大我是老大我是老大"
        android:textColor="#ffffff"
        android:visibility="gone" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#09c0f2"
        android:gravity="center"
        android:text="我是老二"
        android:textColor="#ffffff" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#074bc1"
        android:gravity="center"
        android:text="我是老三"
        android:textColor="#ffffff"
        android:visibility="gone" />

</LinearLayout>

二平均且对齐:

layout_weight属性图解_weight_02


在父控件里添加代码:

android:baselineAligned="false"

三单个分配比例:

layout_weight属性图解_weight_03


在父控件里添加

android:weightSum="2"

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:gravity="center"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:background="#09c0f2"
        android:gravity="center"
        android:text="我是老二"
        android:textColor="#ffffff" />

</LinearLayout>