Android布局——Relative Layout

本文详细介绍了Android开发中的RelativeLayout布局使用方法,包括其基本概念、XML布局示例及如何通过属性设置控件间的相对位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android布局——Relative Layout  

2012-03-21 14:54:58|  分类: Android |  标签:relative  layout  android布局   |字号 订阅

java.lang.Object
   ?android.view.View
    ?android.view.ViewGroup
     ?android.widget.RelativeLayout

 

LinearLayout 是一个ViewGroup,将子View以一种相对位置的方式进行排列。

View位置由与其兄弟View的相对位置(比如在一个View的左边或下面),或者与RelativeLayout区域的相对位置决定(比如与底部对齐)。

在设计UI中,这个布局很重要,因为它可以减少ViewGroups内嵌,如果你发现用到很多LinearLayout布局时,可以考虑使用RelativeLayout代替。

 

1. 创建一个新项目:HelloRelativeLayout

2. 修改文件  res/layout/main.xml

 

<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="fill_parent"
   
android:layout_height="fill_parent">
              
<TextView
                       
android:id="@+id/label"
                       
android:layout_width="fill_parent"
                       
android:layout_height="wrap_content"
                      
android:text="Type here:"/>
           
<EditText
                     
android:id="@+id/entry"
                     
android:layout_width="fill_parent"
                     
android:layout_height="wrap_content"
                    
android:background="@android:drawable/editbox_background"
                    
android:layout_below="@id/label"/>
   
       <Button
       
              android:id="@+id/ok"
                     
android:layout_width="wrap_content"
                     
android:layout_height="wrap_content"
                     
android:layout_below="@id/entry"
                    
android:layout_alignParentRight="true"
                    
android:layout_marginLeft="10dip"
                    
android:text="OK"/>
       
<Button
                    
android:layout_width="wrap_content"
                    
android:layout_height="wrap_content"
                    
android:layout_toLeftOf="@id/ok"
       
             android:layout_alignTop="@id/ok"
                    
android:text="Cancel"/>
</RelativeLayout>

当使用RelativeLayout布局时,可以使用android:layout_* 这些属性[如layout_below, layout_alignParentRight, layout_toLeftOf]来描述View之间的相互位置。

 

3.修改onCreate()方法

publicvoid onCreate(Bundle savedInstanceState){
    
super.onCreate(savedInstanceState);
    setContentView
(R.layout.main);
}

4.运行结果

Android布局——Relative Layout - 老冒儿 - 尝试

 

属性

android:layout_above            A的底线在B(给定ID的View)上方

android:layout_alignBaseline       A与B的底线对齐

android:layout_alignLeft              A与B左边界对齐

android:layout_alignParentBottom    若为"true" A的底部边界与A父View的底部边界对齐

android:layout_alignParentLeft         若为"true" A的左边界与A父View的左边界对齐

android:layout_alignParentRight        若为"true" A的右边界与A父View的右边界对齐

android:layout_alignParentTop       若为"true" A的顶部边界与A父View的顶部边界对齐

android:layout_alignRight               A与B的右边界对齐

android:layout_alignTop

android:layout_alignWithParentIfMissing   若为"true",当使用layout_toLeftOf, layout_toRightOf时,若B找不到,则用A的父View代替

android:layout_below            A在B之下

android:layout_centerHorizontal        若为"true",A在A的父View的水平中央。

android:layout_centerVertical

android:layout_toLeftOf                A的右边界在B的左边

android:layout_toRightOf


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值