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開(kāi)發(fā)之Wifi基礎(chǔ)教程
這篇文章主要介紹了Android開(kāi)發(fā)Wifi基礎(chǔ)教程,實(shí)例分析了Wifi的各種常見(jiàn)基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-02-02
Android開(kāi)發(fā)經(jīng)驗(yàn)談:并發(fā)編程(線(xiàn)程與線(xiàn)程池)(推薦)
這篇文章主要介紹了Android開(kāi)發(fā)經(jīng)驗(yàn)談:并發(fā)編程(線(xiàn)程與線(xiàn)程池),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
詳細(xì)介紹Android中回調(diào)函數(shù)機(jī)制
這篇文章主要介紹了Android中回調(diào)函數(shù)機(jī)制,有需要的朋友可以參考一下2014-01-01
Android開(kāi)發(fā)之如何自定義數(shù)字鍵盤(pán)詳解
這篇文章主要給大家介紹了關(guān)于Android開(kāi)發(fā)之如何自定義數(shù)字鍵盤(pán)的相關(guān)資料,本文語(yǔ)言是基于kotlin實(shí)現(xiàn)的,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-09-09
Android 操作系統(tǒng)獲取Root權(quán)限 原理詳細(xì)解析
許多機(jī)友新購(gòu)來(lái)的Android機(jī)器沒(méi)有破解過(guò)Root權(quán)限,無(wú)法使用一些需要高權(quán)限的軟件,以及進(jìn)行一些高權(quán)限的操作,其實(shí)破解手機(jī)Root權(quán)限是比較簡(jiǎn)單及安全的,破解Root權(quán)限的原理就是在手機(jī)的/system/bin/或/system/xbin/目錄下放置一個(gè)可執(zhí)行文件“su”2013-10-10
Android開(kāi)發(fā)實(shí)現(xiàn)帶清空按鈕的EditText示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)帶清空按鈕的EditText,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)EditText清空按鈕功能相關(guān)操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11
Android Camera2采集攝像頭原始數(shù)據(jù)
這篇文章主要介紹了Android Camera2采集攝像頭原始數(shù)據(jù)并進(jìn)行手工預(yù)覽的功能實(shí)現(xiàn)原理以及代碼分析,需要的朋友學(xué)習(xí)下吧。2018-02-02

