Android開發筆記(二)——佈局管理器
阿新 • • 發佈:2020-07-10
佈局管理器
線性佈局(LinearLayout)
常用屬性
android:id = "@+id/user" android:background = "@mipmap/bg" // 圖片 android:background = "#FF00FF" // 單色 android:layout_width //寬度,wrap_content 包含內容;match_parent 匹配父空間 android:layout_height //高度 android:layout_margin //外邊距 android:layout_marginTop //外邊距頂部 android:layout_marginBottom //外邊距底部 android:layout_marginLeft android:layout_marginRight android:padding //內邊距,四個方向一樣 android:paddingLeft // 不同方向邊距 android:paddingTop android:paddingRight android:paddingBottom android:orientation //該屬性不設定時預設為horizontal(水平),vertical垂直方向 android:gravity //內部元素排列的對齊方式 android:layout_weight //把剩餘內容按照權重分配
測試程式碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:id="@+id/ll_1" android:layout_width="200dp" android:layout_height="200dp" android:orientation="vertical" android:background="#7373B9" android:padding="25dp"> <View android:layout_width="match_parent" android:layout_height="match_parent" android:background="#D1E9E9" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:orientation="horizontal" android:background="#FF5809" android:layout_marginTop="20dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:gravity="center"> <View android:layout_width="50dp" android:layout_height="50dp" android:background="#FFFFFF" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="200dp" android:orientation="horizontal" android:background="#CA8EFF" android:layout_marginTop="20dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:padding="10dp"> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#ACD6FF" android:layout_weight="1" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#FFE66F" android:layout_weight="2" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#FFC1E0" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
效果:
相對佈局(RelativeLayout)
相對佈局特有的屬性:
android:layout_toLeftOf //在誰的左邊
android:layout_toRightOf //右邊
android:layout_alignBottom //
android:layout_below //下面
android:layout_alignParentBottom //和父空間底部對齊(左下角)
測試程式碼:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:id="@+id/view_1" android:layout_width="100dp" android:layout_height="100dp" android:background="#9393FF" /> <View android:id="@+id/view_2" android:layout_width="100dp" android:layout_height="100dp" android:background="#FFFF93" android:layout_below="@id/view_1" /> <LinearLayout android:id="@+id/ll_1" android:layout_width="match_parent" android:layout_height="200dp" android:layout_below="@id/view_2" android:background="#81C0C0" android:orientation="horizontal" android:padding="15dp" > <View android:layout_width="50dp" android:layout_height="match_parent" android:background="#DFFFDF" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFBD9D" android:padding="15dp" > <View android:id="@+id/view_3" android:layout_width="100dp" android:layout_height="match_parent" android:background="#ACD6FF" /> <View android:id="@+id/view_4" android:layout_width="100dp" android:layout_height="match_parent" android:background="#FFE66F" android:layout_toRightOf="@id/view_3" android:layout_marginLeft="15dp" /> </RelativeLayout> </LinearLayout> </RelativeLayout>
效果: