您的位置:首页 > 移动开发 > Android开发

Android学习笔记(二):layout_weight的解读

2015-11-25 12:05 344 查看
1、layout_weight:平分父容器的空间

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text1"
android:text="111111111111111"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"

android:background="#ff782d2d"
android:autoLink="all"
/>
<TextView
android:id="@+id/text2"
android:text="2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#ff1c7824"
android:autoLink="all"
/>
<TextView
android:id="@+id/text3"
android:text="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="#ff192278"
android:autoLink="all"
/>
</LinearLayout>



如图:


layout_weight是,先计算设置的宽度,然后再平分剩下的宽度;

上述代码,3个text都为匹配父类,假设父类width为480,那就是先设置3个text宽为480dp,然后剩下的width为480-480*3=(-480)*2

然后再平分(-480)*2;即使是负的也能平分

text1:480+(-480)*2*(1/5)=480*(3/5)

text2:480+(-480)*2*(2/5)=480*(2/5)

text3:480+(-480)*2*(2/5)=480*(2/5)

2、layout_这个前缀的作用:如果加了layout_,就是由父容器来设置对应的属性;如果不加,就是设置本身内部的属性

eg:layout_gravity:设置其在父容器中的布局位置

gravity:设置本身内部东西的布局位置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: