android學習筆記8:RelativeLayout

來源:互聯網
上載者:User

RelativeLayout為相對布局,這種布局內的組件總是相對兄弟組件、父容器來確定的,在定義控制項的位置時,需要參照其他控制項的位置。

這個程式實現了一個梅花的相對布局

 

 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<!-- 定義該組件位於父容器中間 -->  
<TextView  
    android:id="@+id/view01" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="@drawable/leaf" 
    android:layout_centerInParent="true" 
    /> 
<!-- 定義該組件位於view01組件的上方  與view01靠左對齊--> 
<TextView  
    android:id="@+id/view02" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="@drawable/leaf" 
    android:layout_above="@id/view01" 
    android:layout_alignLeft="@id/view01" 
    /> 
<!-- 定義該組件位於view01組件的下方 --> 
<TextView  
    android:id="@+id/view03"   
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="@drawable/leaf" 
    android:layout_below="@id/view01" 
    android:layout_alignLeft="@id/view01" 
    /> 
<!-- 定義該組件位於view01組件的左邊 --> 
<TextView  
    android:id="@+id/view04" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="@drawable/leaf" 
    android:layout_toLeftOf="@id/view01" 
    android:layout_alignTop="@id/view01" 
    /> 
<!-- 定義該組件位於view01組件的右邊 --> 
<TextView  
    android:id="@+id/view05" 
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:background="@drawable/leaf" 
    android:layout_toRightOf="@id/view01" 
    android:layout_alignTop="@id/view01" 
    />        
</RelativeLayout> 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<!-- 定義該組件位於父容器中間 --> 
<TextView
 android:id="@+id/view01"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/leaf"
 android:layout_centerInParent="true"
 />
<!-- 定義該組件位於view01組件的上方  與view01靠左對齊-->
<TextView
 android:id="@+id/view02"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/leaf"
 android:layout_above="@id/view01"
 android:layout_alignLeft="@id/view01"
 />
<!-- 定義該組件位於view01組件的下方 -->
<TextView
 android:id="@+id/view03" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/leaf"
 android:layout_below="@id/view01"
 android:layout_alignLeft="@id/view01"
 />
<!-- 定義該組件位於view01組件的左邊 -->
<TextView
 android:id="@+id/view04"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/leaf"
 android:layout_toLeftOf="@id/view01"
 android:layout_alignTop="@id/view01"
 />
<!-- 定義該組件位於view01組件的右邊 -->
<TextView
 android:id="@+id/view05"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/leaf"
 android:layout_toRightOf="@id/view01"
 android:layout_alignTop="@id/view01"
 />  
</RelativeLayout>
在activity中動態設定控制項的間距

import android.app.Activity; 
import android.os.Bundle; 
 
public class RelativeLayoutTest extends Activity 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        findViewById(R.id.view01).setPadding(5, 5 , 5 , 5); //設定各個控制項之間的間距  
        findViewById(R.id.view02).setPadding(5, 5 , 5 , 5); 
        findViewById(R.id.view03).setPadding(5, 5 , 5 , 5); 
        findViewById(R.id.view04).setPadding(5, 5 , 5 , 5); 
        findViewById(R.id.view05).setPadding(5, 5 , 5 , 5); 
    } 

 

摘自 hn307165411的專欄

聯繫我們

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