LinearLayout線性布局
1奉狈、線性布局方向有兩個(gè):horizontal央渣、 vertical 默認(rèn)horizontal
horizontal
horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/teal_200"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="LinearLayout布局1"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="LinearLayout布局2"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
vertical
vertical
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/teal_200"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="LinearLayout布局1"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="LinearLayout布局2"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
這里 layout_marginLeft相對(duì)于父視圖而言,layout_marginTop 相對(duì)于上面的 button 而言(如果上面沒(méi)有其他 view 那么就相對(duì)于父視圖射窒,如果有其他 view 那么就相對(duì)于離他最近的 view 來(lái)說(shuō))
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_200"
android:orientation="vertical">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout布局1"
android:textAllCaps="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="LinearLayout布局2"
android:textAllCaps="false" />
</LinearLayout>
LinearLayout 嵌套使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_200"
android:orientation="vertical">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout布局1"
android:textAllCaps="false" />
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="LinearLayout布局2"
android:textAllCaps="false" />
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="20dp"
android:background="@color/red"
android:orientation="horizontal">
<Button
android:id="@+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout布局1"
android:textAllCaps="false" />
<Button
android:id="@+id/button_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="LinearLayout布局2"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
gravity藏杖、layout_gravity的使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_200"
android:gravity="center_vertical"http://設(shè)置全部子視圖垂直方向居中
android:orientation="vertical">
<Button
android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout布局1"
android:layout_gravity="center"http://設(shè)置居中顯示,但是線性布局限制了可布局的高度就是自己的高度上可水平移動(dòng)
android:textAllCaps="false" />//設(shè)置 text 為字母時(shí) 默認(rèn)是大寫(xiě)的 這里設(shè)置為非大寫(xiě)
<Button
android:id="@+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout布局2"
android:textAllCaps="false" />
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="20dp"
android:background="@color/red"
android:layout_gravity="center"http://相對(duì)于外層來(lái)說(shuō)脉顿,該 view 在自己的高度范圍內(nèi)水平居中
android:orientation="horizontal">
<Button
android:id="@+id/button_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayout布局1"
android:textAllCaps="false" />
<Button
android:id="@+id/button_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="LinearLayout布局2"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
RelativeLayout相對(duì)布局
同 LinearLayout 一樣 也可以設(shè)置 gravity 來(lái)設(shè)置子視圖蝌麸。但是子視圖無(wú)法再設(shè)置 layout_gravity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_700">
<Button
android:id="@+id/relative_Btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:text="RelativeLayout布局 1"
android:textAllCaps="false" />
<Button
android:id="@+id/relative_Btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/relative_Btn_1"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@+id/relative_Btn_1"http://這里通過(guò) id 獲取第一個(gè) btn就是相對(duì)于第一個(gè) button 設(shè)置布局
android:text="RelativeLayout布局 2"
android:textAllCaps="false" />
</RelativeLayout>
TableLayout表格布局
從圖中可以看出TableLayout布局默認(rèn)是垂直方向進(jìn)行布局的,而且寬度是填充屏幕的
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個(gè)" />
</TableLayout>
和TableRow結(jié)合使用,發(fā)現(xiàn)超出屏幕部分會(huì)被截掉
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/blue">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個(gè)" />
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個(gè)" />
</TableLayout>
- 和 TableRow 結(jié)合使用艾疟,及 collapseColumns 隱藏列("0,1,2")来吩,stretchColumns拉伸列(3)會(huì)把設(shè)置的拉伸列把剩余的空間拉伸滿(mǎn)。從圖中也可以看出這兩個(gè)屬性的設(shè)置只對(duì)一行多列的情況有作用蔽莱。這里還有一個(gè)情況就是設(shè)置以后如果有多行多列的情況弟疆,那么會(huì)把多行對(duì)應(yīng)的列都給隱藏或拉伸.
-
子控件的兩個(gè)屬性:layout_column在第幾列開(kāi)始顯示;layout_span跨幾列顯示盗冷,就是寬度變成正常列寬的幾倍
設(shè)置collapseColumns = "0,1"
設(shè)置 stretchColumns = "2,3"
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:collapseColumns="0,1"
android:stretchColumns="2,3"
android:background="@color/blue">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個(gè)" />
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4個(gè)" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個(gè)" />
</TableLayout>
GridLayout 網(wǎng)格布局
columnCount設(shè)置一行顯示幾列屬性(用戶(hù)水平布局)
rowCount 設(shè)置一列顯示幾行屬性(用于垂直布局)
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:background="@color/green">
<Button
android:id="@+id/grid_btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個(gè)" />
<Button
android:id="@+id/grid_btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第2個(gè)" />
<Button
android:id="@+id/grid_btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第3個(gè)" />
<Button
android:id="@+id/grid_btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第4個(gè)" />
<Button
android:id="@+id/grid_btn_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個(gè)" />
</GridLayout>
設(shè)置子視圖:layout_row 顯示在第幾行怠苔;layout_column顯示在第幾列;layout_columnSpan橫跨幾列顯示正塌。
以及columnWeight列權(quán)重顯示嘀略, rowWeight 行權(quán)重顯示
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green">
<Button
android:id="@+id/grid_btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第1個(gè)" />
<Button
android:id="@+id/grid_btn_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="第2個(gè)" />
<Button
android:id="@+id/grid_btn_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_column="1"
android:text="第3個(gè)" />
<Button
android:id="@+id/grid_btn_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="第4個(gè)" />
<!--這里有一個(gè)問(wèn)題 設(shè)置最后一個(gè)權(quán)重 rowWeight 沒(méi)有作用?-->
<Button
android:id="@+id/grid_btn_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第5個(gè)" />
</GridLayout>
FrameLayout幀布局
FrameLayout布局兩個(gè)特有屬性 foreground 前景色 foregroundGravity前景色位置
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/red"
android:foreground="@drawable/ic_baseline_person_24"
android:foregroundGravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="FrameLayout學(xué)習(xí) 1"
android:textAllCaps="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="FrameLayout學(xué)習(xí) 2"
android:textAllCaps="false"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
ConstraintLayout約束布局
1乓诽、使用ConstraintLayout設(shè)置 imageview 的時(shí)候需要注意帜羊,設(shè)置 srcCompat圖片資源的時(shí)候,預(yù)覽沒(méi)問(wèn)題鸠天,但是運(yùn)行的時(shí)候就會(huì)看不到圖片資源讼育。因?yàn)槭褂肅onstraintLayout,代碼中使用的是 imageview,但是設(shè)置圖片資源用的是 app:srcCompat奶段。我們知道 imageview 設(shè)置圖片資源的時(shí)候用的是 android:src 所以導(dǎo)致了加載不出來(lái)饥瓷。
2、從 Android23.3 開(kāi)始支持向量 drawable痹籍,只能通過(guò) app:srcCompat進(jìn)行加載了呢铆。
3、解決方案把 imageview 換成AppCompatImageView蹲缠;或者 activity 繼承AppCompatActivity
<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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.067" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/button" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/imageView"
android:layout_width="94dp"
android:layout_height="102dp"
android:layout_marginStart="124dp"
android:adjustViewBounds="true"
android:background="@color/purple_700"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/button2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.246"
app:srcCompat="@drawable/pic1"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>