Android App中的多個(gè)LinearLayout嵌套布局實(shí)例解析
在做android UI布局時(shí),用了LinearLayout嵌套,發(fā)現(xiàn)效果并不如我預(yù)料一般
查了下資料,說是要設(shè)置layout_weight屬性
資料說得不是很清楚,也沒仔細(xì)看,就去弄,結(jié)果越弄越混亂。
于是靜下心來,自己寫xml測試,發(fā)現(xiàn)如下。
如果LinearLayout是最外面的一層,它是不會彈出layout_weight屬性的,
換句話說最外層不能用layout_weight
xml布局如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="zzzzzzzzzzzzzzzzzzzzzzzzzzzzz" android:textSize="18sp" android:layout_marginLeft="5px" android:layout_marginBottom="10px" android:textColor="@android:color/darker_gray" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="xxxxxxxxxxxxxxxxxxxxxxx" android:layout_marginLeft="50px" android:layout_marginBottom="10px" android:textSize="18sp" /> </LinearLayout> </LinearLayout>
這個(gè)能正常顯示,但當(dāng)我們把嵌套的LinearLayout方向設(shè)置成水平,第一個(gè)TextView充滿整個(gè)LinearLayout,第二個(gè)TextView控件不顯示。
當(dāng)我們?yōu)閮蓚€(gè)TextView加上 android:layout_weight="1"屬性時(shí),能顯示,效果我就不說了,大家都懂的。
發(fā)現(xiàn)一個(gè)有趣的現(xiàn)象:我們將 兩個(gè)控件的android:layout_weight="1"刪掉,嵌套的LinearLayout方向?qū)傩詣h掉,代碼和最開始一樣
注意,我們前面說上面的xml它能正常顯示,現(xiàn)在,一模一樣的xml代碼則顯示錯(cuò)誤。
當(dāng)我們只設(shè)置一個(gè)控件的android:layout_weight="1"屬性時(shí),發(fā)現(xiàn)也會有顯示錯(cuò)誤
ps:我只是用可視化工具看了一下 ,并未編譯,說出來,只是告訴大家不要被它的可視化效果誤導(dǎo)(目測是工具的原因)。至于編譯后會如何顯示,這個(gè)有興趣的可以去看下。我說的顯示錯(cuò)誤并不是說文件有錯(cuò)誤,只是在說沒有達(dá)到我想要的效果(都顯示出來)。
更進(jìn)一步,來看這樣一個(gè)效果:

代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/icon_main_sugar" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="水"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
<View
android:layout_width="2dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/icon_main_eat" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="餐具"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
<View
android:layout_width="2dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/icon_main_info" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="信息"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/evaluate_self" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="糖類"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
<View
android:layout_width="2dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/target_manager" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="糖類"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
<View
android:layout_width="2dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/drug_manager" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="糖類"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/news_share" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="新聞"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
<View
android:layout_width="2dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/set_manager" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="設(shè)置"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
<View
android:layout_width="2dip"
android:layout_height="match_parent"
android:background="#E4E4E4" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dip"
android:src="@drawable/content_report" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="3dip"
android:text="任務(wù)"
android:textColor="#7C8187"
android:textSize="15dip" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dip"
android:background="#E4E4E4" />
</LinearLayout>
- Android使用LinearLayout設(shè)置邊框
- Android應(yīng)用借助LinearLayout實(shí)現(xiàn)垂直水平居中布局
- android LinearLayout和RelativeLayout組合實(shí)現(xiàn)精確布局方法介紹
- Android LinearLayout實(shí)現(xiàn)自動(dòng)換行效果
- Android中LinearLayout布局的常用屬性總結(jié)
- Android自定義LinearLayout布局顯示不完整的解決方法
- Android App中使用LinearLayout進(jìn)行居中布局的實(shí)例講解
- Android自定義控件LinearLayout實(shí)例講解
- Android?LinearLayout快速設(shè)置每個(gè)item間隔
相關(guān)文章
Android自定義View 仿QQ側(cè)滑菜單的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android自定義View 仿QQ側(cè)滑菜單的實(shí)現(xiàn)代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-08-08
Android第三方文件選擇器aFileChooser使用方法詳解
這篇文章主要介紹了Android第三方文件選擇器aFileChooser的使用方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
使用kotlin實(shí)現(xiàn)MVP的方式(簡單好用)
這篇文章主要介紹了使用kotlin實(shí)現(xiàn)MVP的方式(簡單好用),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android SDK Manager更新、下載速度慢問題解決辦法
這篇文章主要介紹了Android SDK Manager更新、下載速度慢問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android自定義實(shí)現(xiàn)BaseAdapter的優(yōu)化布局
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的優(yōu)化布局,感興趣的小伙伴們可以參考一下2016-08-08
Android開發(fā)實(shí)現(xiàn)根據(jù)字母快速定位側(cè)邊欄
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)實(shí)現(xiàn)根據(jù)字母快速定位側(cè)邊欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
解決Android Studio 代碼自動(dòng)提示突然失效的問題
這篇文章主要介紹了解決Android Studio 代碼自動(dòng)提示突然失效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android Flutter利用CustomPaint繪制基本圖形詳解
CustomPaint其實(shí)和前端的Canvas基本上是一樣的,前端Canvas支持的繪制方法CustomPaint都支持,畢竟CustomPaint其實(shí)也是基于Canvas實(shí)現(xiàn)的。本篇我們來介紹 CustomPaint 基本圖形的繪制,感興趣的可以了解一下2022-07-07

