Android UI教程 - Android RelativeLayout

2018-01-09 14:51 更新

Android UI教程 - Android RelativeLayout


RelativeLayout允許您指定子視圖如何相對于彼此定位。

考慮下面的main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/RLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <TextView
        android:id="@+id/lblComments"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Comments"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />

    <EditText
        android:id="@+id/txtComments"
           android:layout_width="fill_parent"
           android:layout_height="170px"
           android:textSize="18sp"
           android:layout_alignLeft="@+id/lblComments"
           android:layout_below="@+id/lblComments"
           android:layout_centerHorizontal="true" />
    <Button
           android:id="@+id/btnSave"
           android:layout_width="125px"
           android:layout_height="wrap_content"
           android:text="Save"
           android:layout_below="@+id/txtComments"
           android:layout_alignRight="@+id/txtComments" />

    <Button
           android:id="@+id/btnCancel"
           android:layout_width="124px"
           android:layout_height="wrap_content"
           android:text="Cancel"
           android:layout_below="@+id/txtComments"
           android:layout_alignLeft="@+id/txtComments" />
</RelativeLayout>

null

每個視圖嵌入在 RelativeLayout 中具有使其能夠與另一視圖對齊的屬性。

這些屬性如下:

  • layout_alignParentTop
  • layout_alignParentLeft
  • layout_alignLeft
  • layout_alignRight
  • layout_below
  • layout_centerHorizontal

每個屬性的值是視圖的ID您正在引用。



例子

以下代碼顯示如何使用 RelativeLayout

布局xml

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="5px">
  <TextView android:id="@+id/label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="URL:"
    android:paddingTop="15px"/>
  <EditText
    android:id="@+id/entry"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/label"
    android:layout_alignBaseline="@id/label"/>
  <Button
    android:id="@+id/ok"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/entry"
    android:layout_alignRight="@id/entry"
    android:text="OK" />
  <Button
    android:id="@+id/cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/ok"
    android:layout_alignTop="@id/ok"
    android:text="Cancel" />
</RelativeLayout>

Java代碼

import android.app.Activity;
import android.os.Bundle;
/*from w  ww  .  jav  a 2 s .  c  o m*/
public class RelativeLayoutDemo extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
  }
}
null


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號