標籤:
Android 布局之layout_weight解析
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_weight="1" android:layout_width="0dp" android:gravity="center" android:background="#ff6666" android:layout_height="50dp" android:text="Hello World!" /> <TextView android:layout_weight="1" android:layout_width="0dp" android:gravity="center" android:layout_height="50dp" android:background="#448790" /> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="50dp" android:gravity="center" android:background="#ff00ff" /></LinearLayout>
如上代碼所示,通常我們會用LinearLayout來包住這三個TextView,然後把每個TextView的layout_weight屬性和layout_width屬性分別設為”1”和”0dp”,來實現這種平分的效果:
如果我們想以1:2:3的比例進行展示呢?那我們對代碼稍作修改:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_weight="1" android:layout_width="0dp" android:gravity="center" android:background="#ff6666" android:layout_height="50dp" android:text="Hello World! Hello World!" /> <TextView android:layout_weight="2" android:layout_width="0dp" android:gravity="center" android:layout_height="50dp" android:background="#448790" android:text="1" /> <TextView android:layout_weight="3" android:layout_width="0dp" android:layout_height="50dp" android:gravity="center" android:text="2" android:background="#ff00ff" /></LinearLayout>
不就把layout_weight分別改為1、2、3嘛,so easy, 然而並卵,看結果:
你會發現如果第一個TextView的文案一行還好,一旦超過一行就會顯示得有點詭異了,這尼瑪是什麼情況呢?其實呢,是因為這個LinearLayout它有個基準線,你會發現文本的第一行都是在同一水平線上的,怎麼處理這個問題呢?
LinearLayout有個這個屬性baselineAligned屬性,把它設為false就可以了。
android:baselineAligned="false"
看結果:
再稍微修改下代碼, 把第一個TextView的layout_width設為wrap_content:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:baselineAligned="false" android:orientation="horizontal"> <TextView android:layout_weight="1" android:layout_width="wrap_content" android:gravity="center" android:background="#ff6666" android:layout_height="50dp" android:text="Hfasdlsdhslskdj" /> <TextView android:layout_weight="2" android:layout_width="0dp" android:gravity="center" android:layout_height="50dp" android:background="#448790" android:text="1" /> <TextView android:layout_weight="3" android:layout_width="0dp" android:layout_height="50dp" android:gravity="center" android:text="2" android:background="#ff00ff" /></LinearLayout>
看結果:
發現他會優先把寬度給第一個TextView,然後剩餘的寬度再去安比例分配,也就是第二個TextView占剩餘寬度的2/5,第三個TextView占剩餘寬度的3/5.當然r如果你把第一個TextView的寬度設為100dp,父布局也是把寬度分100dp給第一個TextView,然後剩餘空間再安比例分。也就它父布局會先把減去每個子view的寬度之和,然後剩餘的再去按比例分配。
不知道大家有沒有去思考一個問題,android:layout_width、android:layout_height、android:layout_gravity、android:gravity、android:width、android:height等屬性有什麼區別呢?其實前面加layout的屬性都是由父布局決定的,也就是相當於向父布局申請我要多少寬度,高度,位置要在你的哪邊;而不帶layout的屬性是決定內容寬度、高度、位置。
對於LinearLayout,還有個android:weightSum屬性,他是用來指定總權重的,如果子view的權重超過總權重就會超出螢幕。有些時候,子 view設定了權重,但是有個別子 view顯示不完整,就可以通過設定這個屬性來解決。因為有時候過快的layout會導致父布局來不及計算,所以先給父布局預先設定好總權重就可以避免這種情況。
Android 布局之layout_weight解析