android LinearLayout 布局執行個體代碼

來源:互聯網
上載者:User

先明確幾個概念的區別:
padding margin都是邊距的含義,關鍵問題得明白是什麼相對什麼的邊距.
padding是控制項的內容相對控制項的邊緣的邊距.
margin是控制項邊緣相對父控制項的邊距.

android:gravity 屬性是對該view 內容的限定.比如一個button 上面的text. 你可以設定該text 在view的靠左,靠右等位置.該屬性就幹了這個.
android:layout_gravity是用來設定該view中的子view相對於父view的位置.比如一個button 在linearlayout裡,你想把該button放在靠左,靠右等位置就可以在linearlayout中通過該屬性設定.

 代碼如下 複製代碼

<?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>

例2

 代碼如下 複製代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">

<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

給linearlayout設定邊框 .

1.首先在res目錄下建立一個xml檔案,類型選擇drawable,將自動生一個一個drawable檔案(我用的sdk是android 4.1),並產生一個xml檔案,在其中寫入以下代碼:

 代碼如下 複製代碼

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#FFFFFF" />

    <stroke
        android:width="0.01dp"
        android:color="#FFFFFF" />

    <padding
        android:bottom="1dp"
        android:left="0.5dp"
        android:right="0.5dp"
        android:top="0dp" />
</shape>

2.在要設定邊框的控制項xml命令裡加入:android:background=“@drawable/boder”

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.