android 权重的通俗易懂理解

  权重就是分配剩余空间量。大家在使用权重时候,是不是感觉这个挺难理解的  总感觉朦朦胧胧云里雾里的。今天就给大家一个万能公式,直接套用。

先提出一个公因子概念:公因子 =  要分配控件个数 - 1(1块屏幕)

权重和 = 所有要分配控件权重相加

单个控件最终所占比 = 1 - (公因子*单个权重值)/权重和

如三个按钮权重比为1:2:3,那么要分配控件个数为3公因子为2权重和为6

按钮1 = 1 - 2*1/6 = 2/3

按钮2 = 1 - 2*2/6 = 1/3

按钮3 = 1 - 2*3/6 = 0

三个按钮最终占比为2:1:0

效果如图所示

这是我实现页面底部代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/bottm_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black">

    <LinearLayout
        android:id="@+id/tap01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/zjxx" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tap02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/gzzd" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tap03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/sjjk" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tap04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/gncs" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tap05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/xtbd" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/tap06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/xtsj" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColor="@color/white"/>
    </LinearLayout>

</LinearLayout>

效果如图

猜你喜欢

转载自blog.csdn.net/weixin_54723630/article/details/126106789