日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Android基礎(chǔ):相對布局管理器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.  
  2.     android:layout_width="fill_parent" 
  3.     android:layout_height="fill_parent" 
  4.     android:orientation="vertical" > 
  5.     
  6.         android:id="@+id/img1" 
  7.         android:layout_width="wrap_content" 
  8.         android:layout_height="wrap_content" 
  9.         android:src="@drawable/ic_launcher" /> 
  10.     
  11.         android:id="@+id/img2" 
  12.         android:layout_width="wrap_content" 
  13.         android:layout_height="wrap_content" 
  14.         android:src="@drawable/google_plus" 
  15.         android:layout_toRightOf="@+id/img1" /> 
  16.  

我們在main.xml中將布局管理器聲明為RelativeLayout,之后創(chuàng)建了兩個ImageView組件用來顯示兩幅圖片,其中在第二個 ImageView組件上設(shè)置了layout_toRightOf屬性,也就是設(shè)置相對于某組件的右側(cè),這里填入的是組件ID的值,那么這里也就是說我們 的img2相對于img1的位置是右側(cè)。下面運行程序,我們看到如下效果:

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

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

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

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

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

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

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

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

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

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

 
 
 
 
  1.     android:id="@+id/btn" 
  2.     android:layout_width="wrap_content" 
  3.     android:layout_height="wrap_content" 
  4.     android:text="按鈕" 
  5.     android:layout_below="@+id/txt" 
  6.     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,之后我們可以在代碼中對它進行控制,這里我們在已有 的布局管理器之中繼續(xù)添加組件,也就是要往按鈕下放置一個編輯框,那么我們設(shè)置布局管理器參數(shù)都為FILL_PARENT,就是要填充整個屏幕,然后規(guī)則 定位在btn的下側(cè),之后往布局管理器中添加組件,運行程序,我們就可以看到:


當(dāng)前文章:Android基礎(chǔ):相對布局管理器RelativeLayout
分享地址:http://www.5511xx.com/article/dhsdoee.html