padding margin:都是邊距的含義,關鍵問題得明白是什么相對什么的邊距
padding:是控件的內容相對控件的邊緣的邊距.
margin :是控件邊緣相對父空間的邊距
android:gravity 是對該view 內容的限定.
比如一個button 上面的text. 你可以設置該text 在view的靠左,靠右等位置.該屬性就干了這個.
android:layout_gravity 是用來設置該view中的子view相對于父view的位置.
比如一個button 在linearlayout里,你想把該button放在靠左,靠右等位置就可以在linearlayout中通過該屬性設置
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>
上面布局文件的效果圖
上面的布局文件是一個ListView中的list_item布局,在一個ListView中顯示所有的APK資源,每個資源項顯示圖標,名稱及評分。
在listItem的最外層LinearLayout中加 android:gravity="center_vertical" ,設定內容垂直居中顯示。
在id為 rl_score 的RelativeLayout中設定 android:layout_width="fill_parent" 來填充剩余空間;
android:gravity="right" 設定內容相對于 rl_score 右對齊;
android:padding="10dp"
設定RelativeLayout中的內容相對RelativeLayout的邊緣的邊距為10dp。
這個布局雖然簡單,但卻是經常用到的。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

