自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android基礎(chǔ):相對布局管理器RelativeLayout

移動開發(fā) Android
相對布局管理器是基于一個參考點而言的布局管理器。就像Web開發(fā)中的相對路徑的概念,是基于一定的參考點而創(chuàng)建的。本文附帶圖片示例,詳解相對布局管理器RelativeLayout.

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

下面來看一下RelativeLayout的文檔:

它的繼承結(jié)構(gòu)為:

  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,之后創(chuàng)建了兩個ImageView組件用來顯示兩幅圖片,其中在第二個 ImageView組件上設(shè)置了layout_toRightOf屬性,也就是設(shè)置相對于某組件的右側(cè),這里填入的是組件ID的值,那么這里也就是說我們 的img2相對于img1的位置是右側(cè)。下面運行程序,我們看到如下效果:

很明顯,第二幅圖片放置在了***副圖片的右側(cè),下面往代碼中再加入一個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"/> 

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

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

  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"/> 

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

文字覆蓋***副圖片顯示了,那么需要繼續(xù)對它進(jìn)行設(shè)置:

  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的右側(cè)了。此時文字的下側(cè)和img2的右側(cè)還有一定空間,我們再放置一個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代碼來實現(xiàn)對相對布局管理器的控制,下面首先來看一下RelativeLayout.LayoutParams的文檔:

其繼承結(jié)構(gòu)為:

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

只是在代碼中控制相對布局管理器時需要設(shè)置一些規(guī)則,也就是我們上面看到的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);// 設(shè)置布局管理器參數(shù) 
  17.         params.addRule(RelativeLayout.BELOW, R.id.btn);// 設(shè)置放置規(guī)則 
  18.         EditText edit = new EditText(this);// 創(chuàng)建EditText組件 
  19.         relativeLayout.addView(edit,params); 
  20.     } 

編寫代碼之前,我們需要在main.xml中為我們的布局管理器添加ID屬性,也就是rLayout,之后我們可以在代碼中對它進(jìn)行控制,這里我們在已有 的布局管理器之中繼續(xù)添加組件,也就是要往按鈕下放置一個編輯框,那么我們設(shè)置布局管理器參數(shù)都為FILL_PARENT,就是要填充整個屏幕,然后規(guī)則 定位在btn的下側(cè),之后往布局管理器中添加組件,運行程序,我們就可以看到:

責(zé)任編輯:徐川 來源: ITEYE
相關(guān)推薦

2011-07-04 15:43:03

Qt 布局管理器 designer

2009-02-01 10:17:22

布局管理器LayoutManagGUI

2011-06-10 09:29:36

Qt Creator 布局管理器

2012-04-23 10:49:04

AWTJava

2013-03-29 12:52:14

Android位置管理

2023-11-24 08:21:08

下載任務(wù)管理器

2011-04-19 09:19:37

相對布局界面設(shè)計Android

2010-12-28 14:53:29

服務(wù)器管理器

2021-11-11 11:13:20

js Npm基礎(chǔ)

2009-07-17 09:44:28

Look And Fe布局管理器SWT和Swing

2020-12-25 07:41:36

KubernetesOpenKruise應(yīng)用

2020-03-12 18:34:30

Windows 10Windows任務(wù)管理器

2019-09-05 09:08:53

Android TV文件管理器應(yīng)用軟件

2015-12-08 12:17:04

2022-02-18 08:25:46

微軟Windows 11任務(wù)管理器

2012-12-11 10:10:30

Javaswing.Group

2012-04-27 10:13:30

jQuery Ajax

2023-11-25 09:31:20

Go事件管理器

2023-03-27 10:40:09

2009-03-06 15:27:10

LinuxUbuntuBlueman
點贊
收藏

51CTO技術(shù)棧公眾號