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 布局文件
<?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),名稱及評(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)常用到的。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

