LinearLayout内的权重属性 weight

来源:互联网 发布:财政部ppp中心数据 编辑:程序博客网 时间:2024/05/21 08:42

1、首先设置是横向还是纵向,如果是横向,设置横向width 都是0dp ,weight就是对应的权重,比如,两个按钮  权重分别是1,2,那么第一个按钮占得距离是1/3,第二个是2/3

2、如果对应的宽度或者高度是match,那么计算权重时需要首先计算出总长度或者总高度, 公式=  原宽度+剩余宽度的百分比     

            例如:  两个横向的按钮 宽度都是 match_parent  ,权重分别是1,2  那么第一个按钮的距离=L+(L-2L)*(1/3)= 2/3L


3、如果三个控件使用权重,  

android:baselineAligned="false"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:baselineAligned="false"    android:orientation="horizontal">        <TextView        android:background="#ff0000"        android:gravity="center"        android:layout_width="0dp"        android:layout_height="45dp"        android:layout_weight="1"        android:text="abdcde/ncdd"/>    <TextView        android:background="#0f0"        android:gravity="center"        android:layout_width="0dp"        android:layout_height="45dp"        android:layout_weight="2"        android:text="第二个"/>    <TextView        android:background="#00f"        android:gravity="center"        android:layout_width="0dp"        android:layout_height="45dp"        android:layout_weight="3"        android:text="第三个"/></LinearLayout>



0 0
原创粉丝点击