LinearLayout
线性布局,所谓的线性就是有一定的线性规则的布局。主要可以在水平以及垂直方向线性布局。线性布局也是viewgroup的子类。
水平方向排布
android:orientation=“horizontal”
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
效果
垂直方向线性排布
android:orientation=“vertical”
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"/>
</LinearLayout>
效果
android:layout_gravity属性
设置控件相对于容器的对齐方式,可以中心对齐,右边对齐,左边对齐等等。有如如下属性值设置。这是linerlayout特有的属性
例如设置中心水平对齐
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="two"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
效果,中间的two空间就是中间水平对齐效果,跟word排版差不多
android:layout_weight属性
linerlayout很重要的一个属性,线性布局可以根据比例在水平以及垂直方向上分配空间。
很简单,假如有三个控件,他们的android:layout_weight的分别为a,b,c的值的话,那么他们的布局有一下两种可能.
第一种当:水平方向排布时并且有水平方向match_parent
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
那么他们的水平方向布局大小分别大小
a/(a+b+c)*整个水平方向屏幕大小(主要是因为layout_width)
b/(a+b+c)*整个水平方向屏幕大小
c/(a+b+c)*整个水平方向屏幕大小
那么当layout_width是一个具体的值时,例如
android:layout_width="600dp"
那么他们的水平分布的大小为
a/(a+b+c)*600dp
b/(a+b+c)*600dp
c/(a+b+c)*600dp
第二种当:垂直方向排布时并且有垂直方向match_parent
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
那么他们的垂直方向布局大小分别大小
a/(a+b+c)*整个垂直方向屏幕大小(主要是因为layout_width)
b/(a+b+c)*整个垂直方向屏幕大小
c/(a+b+c)*整个垂直方向屏幕大小
那么当layout_width是一个具体的值时,例如
android:layout_width="600dp"
那么他们的垂直分布的大小为
a/(a+b+c)*600dp
b/(a+b+c)*600dp
c/(a+b+c)*600dp
例子
如下xml,垂直方向上做比重android:layout_weight
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="one"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="two"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="three"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
效果图可以看到是垂直方向上按比例分配控件one two three各占垂直方向上屏幕的1/3