Android基礎:相對布局管理器RelativeLayout

來源:互聯網
上載者:User

相對布局管理器是基於一個參考點而言的布局管理器。就像Web開發中的相對路徑的概念,是基於一定的參考點而建立的。在Android中的相對布局管理器就是在一個參考點的四周上,下,左,右)布局的管理器。

下面來看一下RelativeLayout的文檔:

它的繼承結構為:

 
  1. java.lang.Object 
  2.    ↳ android.view.View 
  3.    ↳ android.view.ViewGroup 
  4.    ↳ android.widget.RelativeLayout 

下面在Eclipse中建立一個項目來看一下相對布局管理器RelativeLayout的使用:

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:orientation="vertical" > 
  6.     <ImageView 
  7.         android:id="@+id/img1" 
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content" 
  10.         android:src="@drawable/ic_launcher" /> 
  11.     <ImageView 
  12.         android:id="@+id/img2" 
  13.         android:layout_width="wrap_content" 
  14.         android:layout_height="wrap_content" 
  15.         android:src="@drawable/google_plus" 
  16.         android:layout_toRightOf="@+id/img1" /> 
  17. </RelativeLayout> 

我們在main.xml中將布局管理器聲明為RelativeLayout,之後建立了兩個ImageView組件用來顯示兩幅圖片,其中在第二個 ImageView組件上設定了layout_toRightOf屬性,也就是設定相對於某組件的右側,這裡填入的是組件ID的值,那麼這裡也就是說我們 的img2相對於img1的位置是右側。下面運行程式,我們看到如下效果:

很明顯,第二幅圖片放置在了第一副圖片的右側,下面往代碼中再加入一個TextView組件:

 
  1. <TextView android:id="@+id/txt" 
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     android:text="這裡是一些顯示文字" 
  5.     android:layout_below="@+id/img2"/> 

這個組件也很簡單,我們設定了layout_below屬性,說明要放置在第二幅圖片的下面,那麼運行程式,我們得到如下的顯示效果:

沒有問題,文字確實在第二幅片的下面了,但是卻頂頭顯示了,如果第一副圖片小於第二幅圖片,是會產生覆蓋效果的,我們調整位置來看一下,調整代碼為:

 
  1. <ImageView 
  2.     android:id="@+id/img1" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" 
  5.     android:src="@drawable/ic_launcher" 
  6.     android:layout_toRightOf="@+id/img2" /> 
  7. <ImageView 
  8.     android:id="@+id/img2" 
  9.     android:layout_width="wrap_content" 
  10.     android:layout_height="wrap_content" 
  11.     android:src="@drawable/google_plus" /> 
  12. <TextView android:id="@+id/txt" 
  13.     android:layout_width="wrap_content" 
  14.     android:layout_height="wrap_content" 
  15.     android:text="這裡是一些顯示文字" 
  16.     android:layout_below="@+id/img1"/> 

這裡不再解釋代碼的含義,直接運行,我們看到:

文字覆蓋第一副圖片顯示了,那麼需要繼續對它進行設定:

 
  1. <TextView android:id="@+id/txt" 
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     android:text="這裡是一些顯示文字" 
  5.     android:layout_below="@+id/img1" 
  6.     android:layout_toRightOf="@+id/img2"/> 

再次運行程式,我們可以看到如下效果:

文字就在img1的下面並且在img2的右側了。此時文字的下側和img2的右側還有一定空間,我們再放置一個Button組件:

 
  1. <Button  
  2.     android:id="@+id/btn" 
  3.     android:layout_width="wrap_content" 
  4.     android:layout_height="wrap_content" 
  5.     android:text="按鈕" 
  6.     android:layout_below="@+id/txt" 
  7.     android:layout_toRightOf="@+id/img2"/> 

再次運行程式,我們就得到了如下效果:

和其它布局管理器一樣,我們可以通過Java代碼來實現對相對布局管理器的控制,下面首先來看一下RelativeLayout.LayoutParams的文檔:

其繼承結構為:

 
  1. java.lang.Object 
  2.    ↳ android.view.ViewGroup.LayoutParams 
  3.    ↳ android.view.ViewGroup.MarginLayoutParams 
  4.    ↳ android.widget.RelativeLayout.LayoutParams  

只是在代碼中控制相對布局管理器時需要設定一些規則,也就是我們上面看到的layout_toRightOf和layout_below等,下面來看一下代碼:

 
  1. package org.ourpioneer; 
  2. import android.app.Activity; 
  3. import android.os.Bundle; 
  4. import android.view.ViewGroup; 
  5. import android.widget.EditText; 
  6. import android.widget.RelativeLayout; 
  7. public class RelativeLayoutDemoActivity extends Activity { 
  8.     @Override 
  9.     public void onCreate(Bundle savedInstanceState) { 
  10.         super.onCreate(savedInstanceState); 
  11.         super.setContentView(R.layout.main);// 讀取已有的布局管理器 
  12.         RelativeLayout relativeLayout = (RelativeLayout) super 
  13.                 .findViewById(R.id.rLayout);// 擷取相對布局管理器rLayout 
  14.         RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( 
  15.                 ViewGroup.LayoutParams.FILL_PARENT, 
  16.                 ViewGroup.LayoutParams.FILL_PARENT);// 設定布局管理器參數 
  17.         params.addRule(RelativeLayout.BELOW, R.id.btn);// 設定放置規則 
  18.         EditText edit = new EditText(this);// 建立EditText組件 
  19.         relativeLayout.addView(edit,params); 
  20.     } 

編寫代碼之前,我們需要在main.xml中為我們的布局管理器添加ID屬性,也就是rLayout,之後我們可以在代碼中對它進行控制,這裡我們在已有 的布局管理器之中繼續添加組件,也就是要往按鈕下放置一個編輯框,那麼我們設定布局管理器參數都為FILL_PARENT,就是要填充整個螢幕,然後規則 定位在btn的下側,之後往布局管理器中添加組件,運行程式,我們就可以看到:

聯繫我們

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