android LinearLayout和RelativeLayout組合實(shí)現(xiàn)精確布局方法介紹
更新時(shí)間:2012年11月12日 14:10:21 作者:
用android LinearLayout和RelativeLayout實(shí)現(xiàn)精確布局此方法適合很適合新人看
先明確幾個(gè)概念的區(qū)別:
padding margin都是邊距的含義,關(guān)鍵問(wèn)題得明白是什么相對(duì)什么的邊距.
padding是控件的內(nèi)容相對(duì)控件的邊緣的邊距.
margin是控件邊緣相對(duì)父空間的邊距.
android:gravity 屬性是對(duì)該view 內(nèi)容的限定.比如一個(gè)button 上面的text. 你可以設(shè)置該text 在view的靠左,靠右等位置.該屬性就干了這個(gè).
android:layout_gravity是用來(lái)設(shè)置該view中的子view相對(duì)于父view的位置.比如一個(gè)button 在linearlayout里,你想把該button放在靠左,靠右等位置就可以在linearlayout中通過(guò)該屬性設(shè)置.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center_vertical">
<ImageView android:id="@+id/ivLogo" android:layout_width="50dp"
android:layout_height="50dp" android:src="@drawable/icon"
android:paddingLeft="5dp" />
<RelativeLayout android:id="@+id/rl_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="right"
android:padding="10dp">
<TextView android:id="@+id/tvApplicationName"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout android:id="@+id/rl_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="right"
android:padding="10dp">
<TextView android:id="@+id/tvRating" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="5.0" />
<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:numStars="5"
style="?android:attr/ratingBarStyleSmall" android:layout_below="@id/tvRating" />
</RelativeLayout>
</LinearLayout>
上面的布局文件是一個(gè)ListView中的list_item布局,在一個(gè)ListView中顯示所有的APK資源,每個(gè)資源項(xiàng)顯示圖標(biāo),名稱(chēng)及評(píng)分。在listItem的最外層LinearLayout中加android:gravity="center_vertical",設(shè)定內(nèi)容垂直居中顯示。在id為rl_score的RelativeLayout中設(shè)定android:layout_width="fill_parent"來(lái)填充剩余空間;android:gravity="right"設(shè)定內(nèi)容相對(duì)于rl_score右對(duì)齊;android:padding="10dp"設(shè)定RelativeLayout中的內(nèi)容相對(duì)RelativeLayout的邊緣的邊距為10dp。
這個(gè)布局雖然簡(jiǎn)單,但卻是經(jīng)常用到的。
padding margin都是邊距的含義,關(guān)鍵問(wèn)題得明白是什么相對(duì)什么的邊距.
padding是控件的內(nèi)容相對(duì)控件的邊緣的邊距.
margin是控件邊緣相對(duì)父空間的邊距.

android:gravity 屬性是對(duì)該view 內(nèi)容的限定.比如一個(gè)button 上面的text. 你可以設(shè)置該text 在view的靠左,靠右等位置.該屬性就干了這個(gè).
android:layout_gravity是用來(lái)設(shè)置該view中的子view相對(duì)于父view的位置.比如一個(gè)button 在linearlayout里,你想把該button放在靠左,靠右等位置就可以在linearlayout中通過(guò)該屬性設(shè)置.
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center_vertical">
<ImageView android:id="@+id/ivLogo" android:layout_width="50dp"
android:layout_height="50dp" android:src="@drawable/icon"
android:paddingLeft="5dp" />
<RelativeLayout android:id="@+id/rl_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:gravity="right"
android:padding="10dp">
<TextView android:id="@+id/tvApplicationName"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="16dp" />
</RelativeLayout>
<RelativeLayout android:id="@+id/rl_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="right"
android:padding="10dp">
<TextView android:id="@+id/tvRating" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="5.0" />
<RatingBar android:id="@+id/ratingbar" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:numStars="5"
style="?android:attr/ratingBarStyleSmall" android:layout_below="@id/tvRating" />
</RelativeLayout>
</LinearLayout>

上面的布局文件是一個(gè)ListView中的list_item布局,在一個(gè)ListView中顯示所有的APK資源,每個(gè)資源項(xiàng)顯示圖標(biāo),名稱(chēng)及評(píng)分。在listItem的最外層LinearLayout中加android:gravity="center_vertical",設(shè)定內(nèi)容垂直居中顯示。在id為rl_score的RelativeLayout中設(shè)定android:layout_width="fill_parent"來(lái)填充剩余空間;android:gravity="right"設(shè)定內(nèi)容相對(duì)于rl_score右對(duì)齊;android:padding="10dp"設(shè)定RelativeLayout中的內(nèi)容相對(duì)RelativeLayout的邊緣的邊距為10dp。
這個(gè)布局雖然簡(jiǎn)單,但卻是經(jīng)常用到的。
您可能感興趣的文章:
- Android使用LinearLayout設(shè)置邊框
- Android應(yīng)用借助LinearLayout實(shí)現(xiàn)垂直水平居中布局
- Android LinearLayout實(shí)現(xiàn)自動(dòng)換行效果
- Android中LinearLayout布局的常用屬性總結(jié)
- Android自定義LinearLayout布局顯示不完整的解決方法
- Android App中使用LinearLayout進(jìn)行居中布局的實(shí)例講解
- Android App中的多個(gè)LinearLayout嵌套布局實(shí)例解析
- Android自定義控件LinearLayout實(shí)例講解
- Android?LinearLayout快速設(shè)置每個(gè)item間隔
相關(guān)文章
Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開(kāi)
這篇文章主要介紹了Android仿QQ好友列表實(shí)現(xiàn)列表收縮與展開(kāi),感興趣的小伙伴們可以參考一下2015-12-12Android View添加 Listener 實(shí)例代碼
在開(kāi)發(fā)中為控件添加Listener是非常常見(jiàn)的工作,最簡(jiǎn)單的添加Listener方式可以這樣2013-05-05Android中fragment+viewpager實(shí)現(xiàn)布局
這篇文章主要為大家詳細(xì)介紹了Android中fragment+viewpager實(shí)現(xiàn)布局效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android Studio 一鍵生成Json實(shí)體類(lèi)教程
這篇文章主要介紹了Android Studio 一鍵生成Json實(shí)體類(lèi)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04RecycleView實(shí)現(xiàn)item側(cè)滑刪除與拖拽
這篇文章主要為大家詳細(xì)介紹了RecycleView實(shí)現(xiàn)item側(cè)滑刪除與拖拽,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Android自定義View繪圖實(shí)現(xiàn)拖影動(dòng)畫(huà)
這篇文章主要介紹了Android自定義View繪圖實(shí)現(xiàn)拖影動(dòng)畫(huà),,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法,涉及Android基于新浪接口的調(diào)用及數(shù)據(jù)處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11