這兩天通過網上的一些視頻教學,了解學習了AS的布局問題,下面對布局進行簡單介紹:
主要包括:
1、LinearLayout(線性布局)
2、View視圖
一、LinearLayout
(1)Layout_width:設置的是當前視圖的寬度,設置的屬性包括match_parent,wrap_content,或者是px值,dp值
match_parent代表的當前視圖繼承父視圖的寬度,如果沒有父視圖,那么繼承當前手機屏幕的寬度。
wrap_content顯示時,按照當前視圖的具體大小來顯示。
或者我們可以給當前視圖直接賦予固定的屬性,常用的單位有px和dp。
px大家都知道是像素點的單位,但是我們日常生活中的手機會 因為款式問題,在像素的分布方面有着較大的區別,所以我們一般會采用手機的計量單位dp。
(2)Layout_height:設置的是當前視圖的高度,其屬性和Layout_width基本一致,這里就不重復了。
(3)Layout_background:設置背景顏色。
(4)orientation:用於設置視圖的牌排列方式,vertical是水平排列,horizotal是垂直排列。
(5)pad:內部元素之間的間隔(pad是四周的間隔,paddingLeft是與左側的間隔,還有其他的paddingRight,paddingButton,paddingStart等,根據直譯即可知道間隔方向)。
(6)gravity:設置對齊方式,例如:center為居中對齊,left為左對齊。
二、View視圖
View視圖包含在LinearLayout中,其主要屬性包括:
(1)Layout_height、Layout_width、Layout_background等屬性和上面提到的基本一致,在這里就不重復了。
(2)layout_weight:是對當前剩余空間按照權重分配(例如1:1的話,會各占剩下部分的50%)。
下面直接上源碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:gravity="left" android:layout_weight="1" > <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#ff9876" android:layout_weight="1" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#00f876" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="30dp" android:paddingLeft="130dp"> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:text="Hello World!"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:gravity="left" android:layout_weight="1" > <View android:layout_width="50dp" android:layout_height="match_parent" android:background="#ffffff" android:layout_weight="1" /> <View android:layout_width="50dp" android:layout_height="match_parent" android:background="#0000ff" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
最后程序運行出的截圖為:
以上就是本次博客的全部內容了,如果有不懂的可以評論詢問哦。