zoukankan      html  css  js  c++  java
  • Android RelativeLayout

    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); 
        } 
    } 
  • 相关阅读:
    GitHub指南
    团队管理之愿景
    软件开发中的思维僵化
    疯狂的 JAVA 后++
    Spring MVC ajax:post/get 的具体实现
    ReentrantLock的Condition使用问题
    Scala学习(一)
    Tomcat源码分析(一)
    LeetCode -- Word Break 动态规划,详细理解
    Javascript 闭包访问问题?
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4282095.html
Copyright © 2011-2022 走看看