layout_weight属性的简识

      今天因为布局需要,用到了这样一个view属性,感觉用的时候挺好用的,这里简单总结一下基本使用

1,基本使用

(1)总布局份额已划分(通常是一个ViewGroup的weightSum给定),比如:

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

    <Button
        android:layout_width="0dp"
        android:text="btn1"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="0dp"
        android:text="btn2"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:text="btn3"
        android:layout_height="wrap_content" />

</LinearLayout>

显示结果(当前屏宽800dp)

上图可以看出,总布局水平分成4份,每一个Button占1份,计算方式:

btn1:  1/4*800=200dp

btn2:  1/4*800=200dp

btn3:  1/4*800=200dp

(2)总份额不给出,则由所有子view的layout:weight属性值之和算为总份额,比如:

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

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="0dp"
        android:text="btn1"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="0dp"
        android:textAllCaps="false"
        android:text="btn2"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="0dp"
        android:textAllCaps="false"
        android:text="btn3"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

</LinearLayout>

显示结果(当前屏宽800dp):

计算方式:

btn1:  1/3*800=800/3dp

btn2:  1/3*800=800/3dp

btn3:  1/3*800=800/3dp

2,特殊使用

(1)部分子view是layout:width指定宽度(高度),部分子view通过layout:weight指定宽度,比如:

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

    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="0dp"
        android:text="btn1"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="0dp"
        android:textAllCaps="false"
        android:text="btn2"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="100dp"
        android:textAllCaps="false"
        android:text="btn3"
        android:layout_height="wrap_content" />

</LinearLayout>

显示结果(当前屏宽800dp):

计算方式:

btn1:  1/2*(800-100)=350dp

btn2:  1/2*(800-100)=350dp

btn3:  100dp

(2)部分子view是layout:width指定宽度(高度),部分子view通过layout:weight指定宽度且总布局份额给定,比如:(这种方式正在考究)

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

    <Button
        android:layout_width="0dp"
        android:text="btn1"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="0dp"
        android:textAllCaps="false"
        android:text="btn2"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="200dp"
        android:textAllCaps="false"
        android:text="btn3"
        android:layout_height="wrap_content" />

</LinearLayout>

显示结果(当前屏宽800dp):

计算方式:

btn1:  1/4*(800-200)=150dp

btn2:  1/4*(800-200)=150dp

btn3:  200dp

(3)部分子view是layout:width指定宽度,部分子view通过layout:weight配合layout:width指定宽度,比如:

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

    <Button
        android:layout_width="0dp"
        android:text="btn1"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="0dp"
        android:textAllCaps="false"
        android:text="btn2"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <Button
        android:layout_width="200dp"
        android:layout_weight="2"
        android:textAllCaps="false"
        android:text="btn3"
        android:layout_height="wrap_content" />

</LinearLayout>

显示结果(当前屏宽800dp):

计算方式:

btn1:  1/4*(800-200)=150dp

btn2:  1/4*(800-200)=150dp

btn3:  200dp+2/4*(800-200)=500dp

(4)部分子view是layout:width指定宽度,部分子view通过layout:weight配合layout:width指定宽度且子view的layout:width指定的总宽度大于屏幕宽度,比如:

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

    <Button
        android:layout_width="match_parent"
        android:text="btn1"
        android:layout_weight="1"
        android:textAllCaps="false"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="600dp"
        android:layout_weight="2"
        android:textAllCaps="false"
        android:text="btn3"
        android:layout_height="wrap_content" />

</LinearLayout>

显示结果(当前屏宽800dp):

计算方式:

btn1:  800+1/3*(800-1400)=600dp

btn3:  600+2/3*(800-1400)=200dp

总结:

子view的宽度计算公式:

layout:width属性值+该子view所占总布局的比例*(总布局宽度-所有子布局通过layout:width属性值定义的宽度之和)

猜你喜欢

转载自blog.csdn.net/hfut_why/article/details/81516473
今日推荐