Android 中权重layout_weight

Android 中权重layout_weight

所谓的权重是按比例分配屏幕吗?

if(wrap_content){

      那么权重越大,所占空间越大,小小不过wrap_content

}

if(match_parent){

      那么权重越大,所占空间越小,大大不过match_parent

}

所以权重不是按比例分配屏幕

权重:将屏幕剩余空间按一定比例分配给空间

假设有两个水平排列Button按钮Btn1 和 Btn2 比例为1:1

屏幕宽度:ScreenWidth

      If按钮为wrap_content类型

      Then

屏幕剩余空间surplusWidth:screenWidth-2*wrap_conten

Btn1宽:wrap_conten+surplusWidth*1/2

Btn2宽:wrap_conten+surplusWidth*1/2

当比例为1:1000时

Btn1宽:wrap_conten+surplusWidth*1/1001

Btn2宽:wrap_conten+surplusWidth*1000/1001

      If按钮为match_parent类型,比例为1:1时

      Then

屏幕剩余空间surplusWidth:screenWidth-2*match_parent

即屏幕剩余空间就是就是一个负值

Btn1宽:match_parent+surplusWidth*1/2

Btn2宽:match_parent+surplusWidth*1/2

当比例为1:1000时

Btn1宽:match_parent+surplusWidth*1/1001

Btn2宽:match_parent+surplusWidth*1000/1001

           因为surplusWidth为负值,所以比例越大,组件越小

如果想通过权重让组件完全按照比例显示该怎么办?

权重是可以让组件按照比例显示的,这种情况就是屏幕的空间等于剩余空间。即对应的宽或高设置为0。

以垂直线性布局为例:android:layout_height="0dp"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

 

    <ImageView

        android:id="@+id/splash_logo_iv"

        android:layout_width="wrap_content"

        android:layout_height="0dp"

        android:layout_weight="15"

        android:src="@drawable/ ic_launcher" />

 

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="0dp"

        android:layout_gravity="center_horizontal"

        android:layout_weight="1"

        android:text="text"/>

</LinearLayout>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值