网友通过本文主要向大家介绍了我的Android进阶之旅------)关于android:layout_weight属性的详细解析等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
我的Android进阶之旅------)关于android:layout_weight属性的详细解析
关于android:layout_weight属性的详细解析
效果一
图1
上面的效果图中三个文本框的宽度比为 1:2:3
图2
代码如下所示:
<code class=" hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/textView1" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="1"> <textview android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2"> <textview android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3"> </textview></textview></textview></linearlayout> </code>
三个TextView的layout_width都设置为0dp,layout_weight的值分别为1:2:3,所以展示的比例也为1:2:3
效果二
图3
图4
看到上面两个图片可以发现:
图3中的第一个文本框与第二、第三个文本框是不对齐的
图4中的第一个文本框与第二、第三个文本框是对齐的
图3的布局代码
下面来看图3的布局代码,如下所示
图5
<code class=" hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:id="@+id/textView1" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="111111111111111111"> <textview android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2"> <textview android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3"> </textview></textview></textview></linearlayout> </code>
图4的布局代码
下面来看图4的布局代码,如下所示
图6
<code class=" hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselinealigned="false"> <textview android:id="@+id/textView1" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="111111111111111111"> <textview android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2"> <textview android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3"> </textview></textview></textview></linearlayout> </code>
可以发现图3和图4的布局文件的差别是在父布局LinearLayout中设置了android:baselineAligned=”false”
效果三
图7
图7代码
<code class=" hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselinealigned="false"> <textview android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="111111111111111111"> <textview android:id="@+id/textView2" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2"> <textview android:id="@+id/textView3" android:layout_width="0dp" android:layout_height="48dp" android:layout_weight="3" android:background="#440000ff" android:gravity="center" android:text="3"> </textview></textview></textview></linearlayout> </code>
图8
图8代码
<code class=" hljs xml"><!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E--> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:baselinealigned="false"> <textview android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="48dp" android:layout_weight="1" android:background="#44ff0000" android:gravity="center" android:text="111111111111111111"> <textview android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="48dp" android:layout_weight="2" android:background="#4400ff00" android:gravity="center" android:text="2"> <t