(原创)关于LinearLayout中layout_we
发布时间: 2013-02-15 15:46:56 作者: rapoo
(原创)关于LinearLayout中layout_weight属性的真相
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="fill_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="100dp"android:orientation="horizontal"><TextViewandroid:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="#aa0000"android:gravity="center"android:text="1" /><TextViewandroid:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="2"android:background="#00aa00"android:gravity="center"android:text="2" /><TextViewandroid:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="3"android:background="#aa0000"android:gravity="center"android:text="3" /></LinearLayout></LinearLayout>复制代码
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"android:layout_height="fill_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="100dp"android:orientation="horizontal"><TextViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:background="#aa0000"android:gravity="center"android:text="1" /><TextViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="2"android:background="#00aa00"android:gravity="center"android:text="2" /></LinearLayout></LinearLayout>复制代码效果如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"? ? android:layout_width="match_parent"? ? android:layout_height="fill_parent"? ? android:orientation="vertical" >? ? <LinearLayout? ?? ???android:layout_width="fill_parent"? ?? ???android:layout_height="100dp"? ?? ???android:orientation="horizontal">? ?? ???<TextView? ?? ?? ???android:layout_width="fill_parent"? ?? ?? ???android:layout_height="fill_parent"? ?? ?? ???android:layout_weight="1"? ?? ?? ???android:background="#aa0000"? ?? ?? ???android:gravity="center"? ?? ?? ?? ?android:text="1"/>? ?? ???<TextView? ?? ?? ???android:layout_width="fill_parent"? ?? ?? ???android:layout_height="fill_parent"? ?? ?? ???android:layout_weight="2"? ?? ?? ???android:background="#00aa00"? ?? ?? ???android:gravity="center"? ?? ?? ???android:text="2"/>? ?? ???? ?? ???<TextView? ?? ?? ???android:layout_width="fill_parent"? ?? ?? ???android:layout_height="fill_parent"? ?? ?? ???android:layout_weight="3"? ?? ?? ???android:background="#00aa00"? ?? ?? ???android:gravity="center"? ?? ?? ???android:text="3"/>? ? </LinearLayout></LinearLayout>复制代码?怎么还是这个效果,不理解!!然后我试试改weight值<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"? ? android:layout_width="match_parent"? ? android:layout_height="fill_parent"? ? android:orientation="vertical" >? ? <LinearLayout? ?? ???android:layout_width="fill_parent"? ?? ???android:layout_height="100dp"? ?? ???android:orientation="horizontal">? ?? ???<TextView? ?? ?? ???android:layout_width="fill_parent"? ?? ?? ???android:layout_height="fill_parent"? ?? ?? ???android:layout_weight="2"? ?? ?? ???android:background="#aa0000"? ?? ?? ???android:gravity="center"? ?? ?? ???android:text="2"/>? ?? ???<TextView? ?? ?? ???android:layout_width="fill_parent"? ?? ?? ???android:layout_height="fill_parent"? ?? ?? ???android:layout_weight="3"? ?? ?? ???android:background="#00aa00"? ?? ?? ???android:gravity="center"? ?? ?? ???android:text="3"/>? ?? ???? ?? ???<TextView? ?? ?? ???android:layout_width="fill_parent"? ?? ?? ???android:layout_height="fill_parent"? ?? ?? ???android:layout_weight="4"? ?? ?? ???android:background="#00aa00"? ?? ?? ???android:gravity="center"? ?? ?? ???android:text="4"/>? ? </LinearLayout></LinearLayout>复制代码
总结:1.要想让weight和你预想的效果一致,子组件的高度或者宽度需要设置为0dp,父容器(LinearLayout)要设置为fill_parent或者确定的值且不能为wrap_content,即mode 为 EXACTLY.?
2.谨慎在 LinearLayout中使用 layout_weight(除非你使用正确), 这样所有子组件的尺寸会被计算两次,将影响性能.?
3.Google的大神们为了赶版本,这个属性的逻辑有点混乱,文档不够详细,给俺们Android丝程序员们埋了好多坑!?
4.关于网上的结论,什么权值大小和重要度成反比,子组件用wrap_content权值才会成正比等,都是错误的!