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"  --将控件的左边缘和给定ID控件的右边缘对齐

android:layout_alignLeft="@id/xxx"  --将控件的左边缘和给定ID控件的左边缘对齐
android:layout_alignTop="@id/xxx"  --将控件的上边缘和给定ID控件的上边缘对齐
android:layout_alignRight="@id/xxx"  --将控件的右边缘和给定ID控件的右边缘对齐
android:layout_alignBottom="@id/xxx"  --将控件的底边缘和给定ID控件的底边缘对齐
android:layout_alignParentLeft="true"  --将控件的左边缘和父控件的左边缘对齐
android:layout_alignParentTop="true"  --将控件的上边缘和父控件的上边缘对齐
android:layout_alignParentRight="true"  --将控件的右边缘和父控件的右边缘对齐
android:layout_alignParentBottom="true" --将控件的底边缘和父控件的底边缘对齐
android:layout_centerInParent="true"  --将控件置于父控件的中心位置
android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置
android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置

编程实例:

定义布局 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/r1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#edab4a" >

<Button

android:id="@+id/Shang"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:text="@string/shang" />

<Button

android:id="@+id/Xia"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_centerHorizontal="true"

android:text="@string/xia" />

<Button

android:id="@+id/Zuo"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_centerVertical="true"

android:text="@string/zuo" />

<Button

android:id="@+id/You"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:text="@string/you" />

<ImageButton

android:contentDescription="@string/wutu"

android:id="@+id/Start"

android:layout_width="60dip"

android:layout_height="100dip"

android:layout_centerInParent="true"

android:src="@drawable/fengjing" />

</RelativeLayout>

字符串资源 sring.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">RelativeLayout</string>

<string name="action_settings">Settings</string>

<string name="hello_world">Hello world!</string>

<string name="shang">上</string>

<string name="xia">下</string>

<string name="zuo">左</string>

<string name="you">右</string>

<string name="wutu">无图</string>

<string name="shuoming">图片说明</string>

</resources>

主活动  MainActivity.java

package com.malakana.relativelayout;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.app.Activity;

public class MainActivity extends Activity {

RelativeLayout r1;

Button shang;

Button xia;

Button zuo;

Button you;

ImageView currButton;

ImageView start;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

r1 = (RelativeLayout) findViewById(R.id.r1);

shang = (Button) findViewById(R.id.Shang);

xia = (Button) findViewById(R.id.Xia);

zuo = (Button) findViewById(R.id.Zuo);

you = (Button) findViewById(R.id.You);

start = (ImageView) findViewById(R.id.Start);

currButton = start;

shang.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//设置控件位置

RelativeLayout.LayoutParams lp_1 =

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.ABOVE,currButton.getId()); //ABOVE  在currButton之上

lp_1.addRule(RelativeLayout.CENTER_HORIZONTAL,currButton.getId());

//将控件添加到布局中

r1.addView(temp,lp_1);

}});

xia.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//设置控件位置

RelativeLayout.LayoutParams lp_1 =

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.BELOW,currButton.getId()); //BELOW  在currButton之下

lp_1.addRule(RelativeLayout.CENTER_HORIZONTAL,currButton.getId());

//将控件添加到布局中

r1.addView(temp,lp_1);

}});

zuo.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//设置控件位置

RelativeLayout.LayoutParams lp_1 =

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.LEFT_OF,currButton.getId()); //LEFT_OF  将控件的右边缘和currButton的左边缘对齐

lp_1.addRule(RelativeLayout.CENTER_VERTICAL,currButton.getId());  //将控件置于垂直方向的中心位置

//将控件添加到布局中

r1.addView(temp,lp_1);

}});

you.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

EditText temp = new EditText(MainActivity.this);

temp.setText(R.string.shuoming);

//设置控件位置

RelativeLayout.LayoutParams lp_1 =

new RelativeLayout.LayoutParams(

ViewGroup.LayoutParams.WRAP_CONTENT,95);

lp_1.addRule(RelativeLayout.RIGHT_OF,currButton.getId()); //RIGHT_OF  将控件的左边缘和currButton的右边缘对齐

lp_1.addRule(RelativeLayout.CENTER_VERTICAL,currButton.getId());  //将控件置于垂直方向的中心位置

//将控件添加到布局中

r1.addView(temp,lp_1);

}});

}

}

效果图:

转载于:https://blog.51cto.com/taiyi928/1550159

Android 界面布局之RelativeLayout相关推荐

  1. Android界面布局练习

    Android界面布局练习 一.实验目的 掌握常用的几种界面布局方法. 能够熟练综合应用各种布局方法进行界面设计. 二.实验内容 制作如下图所示的手机QQ登陆界面. 给控件绑定监听器,当用户点击登陆按 ...

  2. android界面布局

    [url=http://www.cnblogs.com/skynet/archive/2010/06/06/1752616.html]Android 开发之旅:view的几种布局方式及实践[/url] ...

  3. Android相对布局(RelativeLayout)常用属性、练习使用按键、文本框等控件、线性布局(LinearLayout)属性

    RelativeLayout中子控件常用属性: 子控件默认是从父控件的左上角开始排列的 相对于父控件 android:layout_alignParentTop="true" 和父 ...

  4. android storyboard,iOS中xib与storyboard原理,与Android界面布局的异同

    用文本标记语言来进行布局,用的最多的应该是HTML语言.HTML能够理解为有一组特殊标记的XML语言. 一.iOS中xib与storyboard显示原理 在iOS中基本的布置界面的方式有3种:代码.x ...

  5. android 界面布局-各个布局的属性介绍,sharedpreferences原理

    TableLayout tableLayout = (TableLayout) findViewById(R.id.table01); /*创建列对象*/ TableRow tableRow = ne ...

  6. android 相对布局(RelativeLayout)

    RelativeLayout详解: RelativeLayout布局是android布局中最常用的布局之一. RelativeLayout可以设置某一个控件相对于其他控件的位置,这些位置可以包括上下左 ...

  7. android界面布局题,【填空题】Android 系统中, 用于定义布局显示在界面上的风格。...

    [填空题]Android 系统中, 用于定义布局显示在界面上的风格. 更多相关问题 [37]A.anotherB.each otherC.the otherD.one another Tabor ma ...

  8. android 界面布局 很好的一篇总结 【转】

    布局:   在 android 中我们常用的布局方式有这么几种: 1.LinearLayout ( 线性布局 ) :(里面只可以有一个控件,并且不能设计这个控件的位置,控件会放到左上角) 线性布局分为 ...

  9. android 网格界面,Android界面布局(4)—网格布局

    网格布局 网格布局(GridLayout)将用户界面划分为网格,界面元素可以随意摆放在这些网格中.网格布局比表格布局在界面设计上更加灵活,在网格布局中界面元素可以占用多个网格的,而在表格中只能将界面元 ...

最新文章

  1. Servlet3——注解
  2. 远程桌面计算机让输入密码,解决Windows远程桌面连接每次都提示输入密码的问题...
  3. 转:inux shell脚本的字符串截取
  4. ZigBee网络数据传递流程_基于ZigBee—WSN的温湿度监测系统
  5. kotlin 覆盖属性_Kotlin程序| 方法覆盖的示例
  6. Pro*c使用滚动游标进行更新或删除游标行
  7. 国科大高级人工智能12-博弈
  8. VS.Net 2003/VC6.0常用快捷键集合
  9. CSS 设置列表格式
  10. 管理后台--4,删除分类
  11. 18岁华裔准博士生,“杀死了”量子计算大进展
  12. 在边缘计算大热的背景下,为何Akamai敢说自己就是边缘
  13. 解决Intellij中的一些bug
  14. C#抓取网页数据、分析并且去除HTML标签(转载)
  15. python读取二进制文件_python中读写二进制文件
  16. 搞懂激活函数(Sigmoid/ReLU/LeakyReLU/PReLU/ELU)
  17. 计算机毕业设计基于Android的计算器app设计
  18. c语言编程排球队员站位问题,排球比赛的站位技巧介绍,在比赛过程中该如何与队友沟通呢?排球比赛常见的手势说明...
  19. SAP 采购合同案例教程金额合同前台
  20. 通过Nginx访问静态页面

热门文章

  1. com.sun.mail.smtp.SMTPSendFailedException: 550 Invalid User
  2. 原创:centos7.1下 ZooKeeper 集群安装配置+Python实战范例
  3. PHP易混淆函数的区分
  4. iOS自动偏移64个像素
  5. ATG中的定时Job处理
  6. ASP中利用OWC控件实现图表功能详解[zz]
  7. 我会说我喜欢创业嘛?(每个月总有几天会更新…………标题一定要长)
  8. PMcaff会员圣诞礼物大放送!
  9. 【pmcaff】凯文-凯利:最大颠覆来自创业公司与边缘产业
  10. python3.6入门到高阶(全栈) day02 while循环 运算符 格式化输出 编码