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開發(fā)注解排列組合出啟動(dòng)任務(wù)ksp
這篇文章主要為大家介紹了Android開發(fā)注解排列組合出啟動(dòng)任務(wù)ksp示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Android仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄的問題
這篇文章主要介紹了Android之仿微信QQ聊天頂起輸入法不頂起標(biāo)題欄問題,本文實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2
這篇文章主要介紹了Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2,ViewPager2最顯著的特點(diǎn)是基于RecyclerView實(shí)現(xiàn),RecyclerView是目前Android端最成熟的AdapterView解決方案2023-03-03
Android中Fab(FloatingActionButton)實(shí)現(xiàn)上下滑動(dòng)的漸變效果
這篇文章主要給大家介紹了Android中FloatingActionButton(簡稱FAB)是如何實(shí)現(xiàn)上下滑動(dòng)的漸變效果,文中給出了詳細(xì)的示例代碼,相信對大家具有一定的參考價(jià)值,有需要的朋友們可以一起看看吧。2017-02-02
AndroidStudio項(xiàng)目打包成jar的簡單方法
JAR(Java Archive,Java 歸檔文件)是與平臺無關(guān)的文件格式,它允許將許多文件組合成一個(gè)壓縮文件,在eclipse中我們知道如何將一個(gè)項(xiàng)目導(dǎo)出為jar包,供其它項(xiàng)目使用呢?下面通過本文給大家介紹ndroidStudio項(xiàng)目打包成jar的簡單方法,需要的朋友參考下吧2017-11-11
Android實(shí)現(xiàn)百度地圖兩點(diǎn)畫弧線
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)百度地圖兩點(diǎn)畫弧線,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android開發(fā)之StackView用法和遇到的坑分析
這篇文章主要介紹了Android開發(fā)之StackView用法和遇到的坑,結(jié)合實(shí)例形式分析了Android StackView圖片操作用法及常見問題解決方法,需要的朋友可以參考下2019-03-03
圖文詳解Android Studio搭建Android集成開發(fā)環(huán)境的過程
這篇文章主要以圖文的方式詳細(xì)介紹了Android Studio搭建Android集成開發(fā)環(huán)境的過程,文中安裝步驟介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12

