软工日报

(一)线性布局LinearLayout
线性布局在开发中使用最多,具有垂直方向与水平方向的布局方式,通过设置属性“android:orientation”控制方向,属性值垂直(vertical)和水平(horizontal),默认水平方向。
android:gravity:内部控件对齐方式,常用属性值有center、center_vertical、center_horizontal、top、bottom、left、right等。
这个属性在布局组件RelativeLayout、TableLayout中也有使用,FrameLayout、AbsoluteLayout则没有这个属性。
center:居中显示,这里并不是表示显示在LinearLayout的中心,当LinearLayout线性方向为垂直方向时,center表示水平居中,但是并不能垂直居中,此时等同于center_horizontal的作用;同样当线性方向为水平方向时,center表示垂直居中,等同于center_vertical。
top、bottom、left、right顾名思义为内部控件居顶、低、左、右布局。
这里要与android:layout_gravity区分开,layout_gravity是用来设置自身相对于父元素的布局。
android:layout_weight:权重,用来分配当前控件在剩余空间的大小。
使用权重一般要把分配该权重方向的长度设置为零,比如在水平方向分配权重,就把width设置为零。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<!--
第一线性布局
LinearLayout一定要设置方向
android:orientation 值:horizontal 元素水平摆放 | vertical 元素垂直摆放
android:gravity="right"设置对齐方式
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center" >

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消" />
</LinearLayout>

<!-- 第二线性布局 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#ff0000"
android:text="红色"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#00ff00"
android:text="绿色"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:background="#0000ff"
android:text="蓝色"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="2"
android:background="#00ffff"
android:text="青色"/>
</LinearLayout>
</LinearLayout>

posted @ 2023-04-25 17:34  ZzHhyao  阅读(20)  评论(0)    收藏  举报