Android之布局屬性重點

來源:互聯網
上載者:User

先明確幾個概念的區別: 

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。 
這個布局雖然簡單,但卻是經常用到的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.