Android之Linearlayouy线性布局

时间:2023-03-09 06:54:44
Android之Linearlayouy线性布局
  • 写了个小例子xml代码如下:
 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fnst.linearlayout.MainActivity"
tools:showIn="@layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:gravity="center_horizontal"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:gravity="center_horizontal"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button2"/>
<Button
android:gravity="center_horizontal"
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button3"/>
</LinearLayout> <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button4"/>
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button5"/>
<Button
android:id="@+id/button6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button6"/>
</LinearLayout>
</LinearLayout> <!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Hello World!" />-->
</RelativeLayout>
  • 运行结果如图:

Android之Linearlayouy线性布局

  • 这里有一比较奇怪的地方:

当二级的Linearlayout节点的layout_width,layout_height属性值是fill_parent或match_parent(因为版本间的兼容性问题建议使用),此时layout_weight的权重值与屏幕布局宽或高是成反比的。比如:

        <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<Button
android:id="@+id/button1"/>
<Button
android:id="@+id/button2"/>
<Button
android:id="@+id/button3"/>
</LinearLayout> <LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:id="@+id/button1"/>
<Button
android:id="@+id/button2"/>
<Button
android:id="@+id/button3"/>
</LinearLayout>

此时上面的Linearlayout占1/3,此时他的权重是2,下面的Linearlayout占2/3他的权重是1。

当二级的Linearlayout节点的layout_width,layout_height属性值是wrap_conten时权重值和布局宽度或长度成正比。

修改xml如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.fnst.linearlayout.MainActivity"
tools:showIn="@layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2">
<Button
android:gravity="center_horizontal"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button1"/>
<Button
android:gravity="center_horizontal"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button2"/>
<Button
android:gravity="center_horizontal"
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button3"/>
</LinearLayout> <LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button4"/>
<!--<Button-->
<!--android:id="@+id/button5"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_weight="1"-->
<!--android:text="Button5"/>-->
<!--<Button-->
<!--android:id="@+id/button6"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_weight="1"-->
<!--android:text="Button6"/>-->
</LinearLayout>
</LinearLayout> <!--<TextView-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Hello World!" />-->
</RelativeLayout>

运行结果如下图:

Android之Linearlayouy线性布局