Android UI布局之RelativeLayout

RelativeLayout是一个相对布局类。首先RelativeLayout是一个容器,它里边的元素,如Button按钮等的位置是按照相对位置来计算的,例如,有两个Button按钮都布局在一个RelativeLayout里边,我们可以定义第二个Button在第一个Button的上边或者是右边。但到底第二个Button在什么位置呢,它还是依赖于第一个Button的位置。需要注意的是,出于性能上的考虑,对于相对布局的精确位置的计算只会执行一次,所以,如果一个可视化组件B依赖A,那么必须要让A出现在B的前边。

实例:LayoutDemo

运行效果:

代码清单:

布局文件:relative_layout.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="wrap_content"
    android:padding="10dip"
    >
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="请输入用户名:"
    />
    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/label"
    />
    <Button
        android:id="@+id/cancel"
        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="取消"
    />
    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/cancel"
        android:layout_alignTop="@id/cancel"
        android:text="确定"
    />

</RelativeLayout>

Java源代码文件:RelativeLayoutActivity.java

package com.rainsong.layoutdemo;

import android.app.Activity;
import android.os.Bundle;

public class RelativeLayoutActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.relative_layout);
    }
}

API知识点

android:id                  定义组件的id

android:layout_width        定义组件的宽度

android:layout_height       定义组件的高度

android:padding             填充

android:layout_below        将当前组件放置于指定组件的下方

android:layout_alignParentRight 和父容器的右边齐平

android:layout_marginLeft   右边距

android:layout_toLeftOf     设置此组件在指定组件的左边

android:layout_alignTop     设置此组件和指定组件高度齐平

时间: 2024-08-15 08:24:53

Android UI布局之RelativeLayout的相关文章

android UI布局

一.设置重复背景 在drawable文件夹下建一个mybackground.xml文件 在文件中写入: <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/mybg_img" android:tileMod

Android 界面布局之RelativeLayout

Android 的 RelaliveLayout 布局的参数定义: android:layout_above="@id/xxx"  --将控件置于给定ID控件之上android:layout_below="@id/xxx"  --将控件置于给定ID控件之下 android:layout_toLeftOf="@id/xxx"  --将控件的右边缘和给定ID控件的左边缘对齐android:layout_toRightOf="@id/xxx&

Android UI布局与控件及API Guide学习(一)

一.Android学习API指南:[了解] 1. 应用的组成部分   App Components 1.1. 应用的基本原理    App Fundamentals 1.2. Activity      Activities活动 1.2.1. 片段    Fragments 1.2.2. 加载器     Loaders 1.2.3. 任务和返回堆    Tasks and Back Stack 1.3. Service服务   Services 1.3.1. 绑定服务     Bound Ser

Android UI布局与控件(二)

一.View类的常用xml属性:[了解] ①.Android中所有的UI(用户界面)元素都是使用View和ViewGroup对象建立的 ②.View是一个可以将一些信息绘制在屏幕上并与用户产生交互的对象 ③.ViewGroup是一个包含多个的View和ViewGroup的容器,用来定义UI布局. ④.Android提供了一系列的View和ViewGroup的子类,开发者可以灵活地组合使用它们来完成界面布 局.界 面元素绘制和用户交互等工作 ⑤.开发者还可以选择性地继承一些系统提供的View,来自

关于android UI布局自适应

原以为上篇就是农历年到来前写的最后一篇了,但是看来现在还是有必要继续把看到的有用的记录一下,算是比较基础的了,以前没怎么关注. 言归正传,这篇主要说下android的自适应的一点东西,为什么会忽然想起来这个,主要还是因为之前看launcher的代码,其中看到的这段代码始终不明白,所以查了下资料.看下代码, BitmapDrawable bitmapDrawable = (BitmapDrawable) icon; Bitmap bitmap = bitmapDrawable.getBitmap(

Android UI -- 布局介绍(布局包括FrameLayout, LinearLayout, RelativeLayout, GridLayout)

首先介绍常用布局类 FrameLayout 最简单的布局管理器. 这个布局管理类有几个特性: 添加组件默认在左上角的. 如果添加多个组件会叠加到一起,并且都在左上角.(可以通过一gravity属性改变叠加情况) 后添加的组件在上层. LinearLayout LinearLayout通过垂直方向和水平方面进行布局的,LinearLayout允许每一个视图都有一个weight属性. RelativeLayout RelativeLayout是相对于其他的组件和屏幕边缘布局的管理类. GridLay

ANDROID UI布局学习

一.LINEARLAYOUT 线性布局 android:layout_width=""match_parent 适应父控件 fill_parent 填充父控件 wrap_content 内容包裹 android:orientation="" 方向选择 vertical 竖向 horizontal 横向 android:laytou_weight="" 权重 二.FRAMELAYOU 框架布局 三.RELATIVELAYOUT 相对布局 andro

Android UI布局之LinearLayout

LinearLayout是Android中最常用的布局之一,它将自己包含的子元素按照一个方向进行排列.方向有两种,水平或者竖直.这个方向可以通过设置android:orientation="vertical"或者android:orientation="horizontal"来实现,所有的元素排列都是一个接着一个的.如果是竖直排列,那么LinearLayout的元素就一个接着一个的从上到下竖直排列,例如,在下面的例子中,MainActivity的视图就是这样竖直的一

Android UI布局经验总结

1.  画UI的原则 能简则简,能用一个控件搞定的事情,不要用多个控件. 能抽则抽,可以抽取复用的属性或布局就抽取. 3.  资源     在android项目里,资源放在res文件夹下,资源可以是图片.xml等,不同类型的资源放在不同 的文件下,如下图所示 引用的时候可以@[android:]anim/ @[android:]drawable/ @[android:]layout/ @[android:]menu/ @[android:]layout/ @[android:]menu/ @[a