視頻鏈接 : http://www.imooc.com/video/10165
1.看第一個例子,當我們按比例放置多個TextView時,在沒有添加內容或者內容沒有超過分配的寬度時,這些TextView會很正常的顯示在屏幕上,但是當某一個TextView 的內容寬度多于分配的寬度時, 就會 出現 如下情況。
~~~
<?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="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="@color/person_center_item1"
android:gravity="center"
android:text="1234646156" />
<TextView
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="2"
android:background="@color/person_center_item2"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="3"
android:background="@color/person_center_item3"
android:gravity="center"
android:text="3" />
</LinearLayout>
~~~

出現這種情況是因為 他們的對齊當時是根據父控件的基線進行對齊的,很明顯的可以看到123..... 和 2 ,3 的下邊界是在一條線上的。解決這種問題 只要這 ?上面代碼的LinearLayout 中添加如下代碼就可以了。android:baselineAligned="false"

2.當我們把上面代碼 的第一個TextView 改成?wrap_content?之后?,我們會發現 第一個TextView 原本只占1/6寬度的,現在卻占了一半左右的寬度。這是為什么呢?

這是因為在分配寬度的時候 ,首先會給每個TextView分配他們自己layout_width 值的寬度,然后將剩下的按照比列依次加給3個控件。
3.然后我們把3個TextView的寬度全部改成?match_parent,然后我們發現屏幕上只有第一、二個TextView了,第三個不見了。這又是為什么呢?

原因和第2點一樣,首先給每個TextView分配?match_parent的寬度,然后再把剩下的寬度加到每個控件上。我們假設屏幕寬度為480, 第一次分配的寬度為 480 * 3; 然后 用屏幕寬度 減去 第一次分配的寬度,也就是480-480*3 得到的是 -960, 然后把 -960 分成6份.
1/6 加到第一個TextView上: 480 +(-960 /6) = 320;
2/6 加到第一個TextView上: 480 +(-960 /6)* 2 = 160;
3/6 加到第一個TextView上: 480 +(-960 /6)* 3 = 0;
第三個TextView為0,所以不顯示。
4.當我們想在LinearLayout中讓一個TextView 占一半屏幕寬度的時候,我們可以這樣做:讓TextView的layout_weight值為1,或者其他你喜歡的,然后在父控件LinearLayout中聲明?**android****:weightSum=****"2"**或者自己定義值得2倍就OK了!效果如下。
??

5.對于以 layout_XXX,這樣格式的屬性設置,這些屬性是交給父控件去分配的 ,像layout_width、layout_height等,像layout_gravity="center",就是 將該控件置于父控件的中心位置。而那些沒有以layout_開始的屬性則是控件自身控制和分配的。
- 前言
- 面試中關于 layout_weight 筆記
- 關于安裝APK到Genymation 模擬器報 install failed cpu abi incompatible
- Error:Execution failed for task ':app:packageDebug'. > Duplicate files copied in APK META-INF/LICENS
- 仿QQ6.1手勢鎖
- 當expandlistview的getGroupView或者getChildView中包含checkbox時,前者點擊不可用
- 在genymotion官網下載genymotion-2.6.0-vbox.exe安裝完成模擬器不可用的問題解決
- android studio logcat 打印不出信息
- 去掉友盟分享里面不需要的平臺
- 在有EditText的界面 默認情況下不獲取焦點(不彈出輸入框)
- android studio 導入第三方庫的記錄
- Error:Execution failed for task ':app:transformClassesWithDexForDebug'解決記錄
- Edittext In Listview,當listview的item中有edittext時,怎么保存edittext的值?