Android UI布局之RelativeLayout

來源:互聯網
上載者:User

標籤:android   relativelayout   

RelativeLayout是一個相對布局類。首先RelativeLayout是一個容器,它裡邊的元素,如Button按鈕等的位置是按照相對位置來計算的,例如,有兩個Button按鈕都布局在一個RelativeLayout裡邊,我們可以定義第二個Button在第一個Button的上邊或者是右邊。但到底第二個Button在什麼位置呢,它還是依賴於第一個Button的位置。需要注意的是,出於效能上的考慮,對於相對布局的精確位置的計算只會執行一次,所以,如果一個可視化組件B依賴A,那麼必須要讓A出現在B的前邊。


執行個體:LayoutDemo
運行效果:

代碼清單:
布局檔案:relative_layout.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:padding="10dip"    >    <TextView        android:id="@+id/label"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="請輸入使用者名稱:"    />    <EditText         android:id="@+id/entry"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/label"    />    <Button        android:id="@+id/cancel"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/entry"        android:layout_alignParentRight="true"        android:layout_marginLeft="10dip"        android:text="取消"     />    <Button        android:id="@+id/ok"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_toLeftOf="@id/cancel"        android:layout_alignTop="@id/cancel"        android:text="確定"     /></RelativeLayout>
Java原始碼檔案:RelativeLayoutActivity.java
package com.rainsong.layoutdemo;import android.app.Activity;import android.os.Bundle;public class RelativeLayoutActivity extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.relative_layout);    }}


API知識點

android:id                  定義組件的id
android:layout_width        定義組件的寬度
android:layout_height       定義組件的高度
android:padding             填充
android:layout_below        將當前組件放置於指定組件的下方
android:layout_alignParentRight 和父容器的右邊齊平
android:layout_marginLeft   右邊距
android:layout_toLeftOf     設定此組件在指定組件的左邊
android:layout_alignTop     設定此組件和指定組件高度齊平


Android UI布局之RelativeLayout

聯繫我們

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