xml文件实现

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="500dp" > <Button
android:id="@+id/btn_center"
style="@style/btn_relative"
android:layout_centerInParent="true"
android:text="我在中间" /> <Button
android:id="@+id/btn_center_horizontal"
style="@style/btn_relative"
android:layout_centerHorizontal="true"
android:text="我在水平中间" /> <Button
android:id="@+id/btn_center_vertical"
style="@style/btn_relative"
android:layout_centerVertical="true"
android:text="我在垂直中间" /> <Button
android:id="@+id/btn_parent_left"
style="@style/btn_relative"
android:layout_marginTop="100dp"
android:layout_alignParentLeft="true"
android:text="我跟上级左边对齐" /> <Button
android:id="@+id/btn_parent_top"
style="@style/btn_relative"
android:layout_width="120dp"
android:layout_alignParentTop="true"
android:text="我跟上级顶部对齐" /> <Button
android:id="@+id/btn_parent_right"
style="@style/btn_relative"
android:layout_marginTop="100dp"
android:layout_alignParentRight="true"
android:text="我跟上级右边对齐" /> <Button
android:id="@+id/btn_parent_bottom"
style="@style/btn_relative"
android:layout_width="120dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="我跟上级底部对齐" /> <Button
android:id="@+id/btn_left_bottom"
style="@style/btn_relative"
android:layout_toLeftOf="@+id/btn_parent_bottom"
android:layout_alignTop="@+id/btn_parent_bottom"
android:text="我在底部左边" /> <Button
android:id="@+id/btn_right_bottom"
style="@style/btn_relative"
android:layout_toRightOf="@+id/btn_parent_bottom"
android:layout_alignBottom="@+id/btn_parent_bottom"
android:text="我在底部右边" /> <Button
android:id="@+id/btn_above_center"
style="@style/btn_relative"
android:layout_above="@+id/btn_center"
android:layout_alignLeft="@+id/btn_center"
android:text="我在中间上面" /> <Button
android:id="@+id/btn_below_center"
style="@style/btn_relative"
android:layout_below="@+id/btn_center"
android:layout_alignRight="@+id/btn_center"
android:text="我在中间下面" /> </RelativeLayout>

java

 package com.example.alimjan.hello_world;

 import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import com.example.alimjan.hello_world.two.class__2_1; /**
* Created by alimjan on 7/2/2017.
*/ public class class_3_1_1 extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_3_1_1);
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_3_1_1.class);
mContext.startActivity(intent);
}
}

代码实现

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_content"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
style="@style/btn_relative"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="点击按钮添加视图,长按视图删除自身" /> <Button
android:id="@+id/btn_add_left"
style="@style/btn_relative"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:text="添加左边视图" /> <Button
android:id="@+id/btn_add_above"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_left"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加上方视图" /> <Button
android:id="@+id/btn_add_right"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_above"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加右边视图" /> <Button
android:id="@+id/btn_add_below"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_right"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加下方视图" /> <Button
android:id="@+id/btn_add_center"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_below"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加中间视图" /> <Button
android:id="@+id/btn_add_parent_left"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_center"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加上级左侧对齐视图" /> <Button
android:id="@+id/btn_add_parent_top"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_parent_left"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加上级顶部对齐视图" /> <Button
android:id="@+id/btn_add_parent_right"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_parent_top"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加上级右侧对齐视图" /> <Button
android:id="@+id/btn_add_parent_bottom"
style="@style/btn_relative"
android:layout_below="@+id/btn_add_parent_right"
android:layout_alignLeft="@+id/btn_add_left"
android:text="添加上级底部对齐视图" /> </RelativeLayout>

 package com.example.alimjan.hello_world;

 import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RelativeLayout; /**
* Created by alimjan on 7/2/2017.
*/ public class class_3_1_1_code extends AppCompatActivity implements View.OnClickListener { private RelativeLayout rl_content; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_3_1_1_code); rl_content = (RelativeLayout) findViewById(R.id.rl_content);
findViewById(R.id.btn_add_left).setOnClickListener(this);
findViewById(R.id.btn_add_above).setOnClickListener(this);
findViewById(R.id.btn_add_right).setOnClickListener(this);
findViewById(R.id.btn_add_below).setOnClickListener(this);
findViewById(R.id.btn_add_center).setOnClickListener(this);
findViewById(R.id.btn_add_parent_left).setOnClickListener(this);
findViewById(R.id.btn_add_parent_top).setOnClickListener(this);
findViewById(R.id.btn_add_parent_right).setOnClickListener(this);
findViewById(R.id.btn_add_parent_bottom).setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_add_left) {
addNewView(RelativeLayout.LEFT_OF, RelativeLayout.ALIGN_TOP, v.getId());
} else if (v.getId() == R.id.btn_add_above) {
addNewView(RelativeLayout.ABOVE, RelativeLayout.ALIGN_LEFT, v.getId());
} else if (v.getId() == R.id.btn_add_right) {
addNewView(RelativeLayout.RIGHT_OF, RelativeLayout.ALIGN_BOTTOM, v.getId());
} else if (v.getId() == R.id.btn_add_below) {
addNewView(RelativeLayout.BELOW, RelativeLayout.ALIGN_RIGHT, v.getId());
} else if (v.getId() == R.id.btn_add_center) {
addNewView(RelativeLayout.CENTER_IN_PARENT, -1, rl_content.getId());
} else if (v.getId() == R.id.btn_add_parent_left) {
addNewView(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.CENTER_VERTICAL, rl_content.getId());
} else if (v.getId() == R.id.btn_add_parent_top) {
addNewView(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.CENTER_HORIZONTAL, rl_content.getId());
} else if (v.getId() == R.id.btn_add_parent_right) {
addNewView(RelativeLayout.ALIGN_PARENT_RIGHT, -1, rl_content.getId());
} else if (v.getId() == R.id.btn_add_parent_bottom) {
addNewView(RelativeLayout.ALIGN_PARENT_BOTTOM, -1, rl_content.getId());
}
} private void addNewView(int firstAlign, int secondAlign, int referId) {
View v = new View(this);
v.setBackgroundColor(0xaa66ff66);
RelativeLayout.LayoutParams rl_params = new RelativeLayout.LayoutParams(100, 100);
rl_params.addRule(firstAlign, referId);
if (secondAlign >= 0) {
rl_params.addRule(secondAlign, referId);
}
v.setLayoutParams(rl_params);
v.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View vv) {
rl_content.removeView(vv);
return true;
}
});
rl_content.addView(v);
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_3_1_1_code.class);
mContext.startActivity(intent);
} }

Android 开发笔记___RelativeLayout的更多相关文章

  1. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  2. Android开发笔记--hello world 和目录结构

    原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...

  3. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  4. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

  5. [APP] Android 开发笔记 002-命令行创建默认项目结构说明

    接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.

  6. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  7. Android开发笔记(一百三十四)协调布局CoordinatorLayout

    协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...

  8. 【转】Android开发笔记——圆角和边框们

    原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...

  9. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...

随机推荐

  1. SSM之框架整合

    前言 SSM框架,即Spring + Spring MVC + MyBatis的整合框架集,是继SSH后比较主流的Java EE企业级框架,采用标准的MVC模式,项目结构与微软的ASP.NET MVC ...

  2. 分享ES6中比较常用又强大的新特性

    前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...

  3. Java面向对象 线程技术--上篇

     Java面向对象   线程 知识概要:                   (1)线程与进程 (2)自定义线程的语法结构 (3)多线程概念理解 (4)多线程状态图 (5)多线程--卖票 (6)同 ...

  4. MySQL的备份与还原以及常用数据库查看命令

    MySQL命令行导出数据库: 1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Serv ...

  5. 强大的grep,sed和awk--用案例来讲解

    准备工作: 先简单了解grep,sed和awk功能 1) grep 显示匹配特定模式的内容 grep -v 'boy' test.txt 过滤掉test.txt文件的boy,显示其余内容 grep ' ...

  6. Virgo标签打印

    去年刚换新的公司,熟悉新的业务和代码,在修改公司打印标签的时候,感觉到无比烦躁与头痛.只因为不好维护,所有的标签打印,全部是GDI+绘制,每次修改微调,都只能全部运行才能看到效果.程序过大,编译过慢, ...

  7. SpringMVC 实现文件的上传与下载

    一  配置SpringMVC ,并导入与文件上传下载有关的jar包(在此不再赘述) 二 新建 相应 jsp 和controller FileUpAndDown.jsp <%@ page lang ...

  8. Thrift总结(三)Thrift框架

    1.数据类型 基本类型: bool:布尔值,true 或 false,对应 Java 的 boolean byte:8 位有符号整数,对应 Java 的 byte i16:16 位有符号整数,对应 J ...

  9. 利用大白菜制作多系统启动U盘(win+ubuntu+PE+...)

    网上提供的方法很多都过时了,不适用,要不就是讲的不清楚 我结合http://www.xuebuyuan.com/848003.html大神的方案,加以研究,整理出了此篇文章 先看下最终成果: 好了,感 ...

  10. word2vec 数学原理

    word2vec 是 Google 于 2013 年推出的一个用于获取词向量的开源工具包.我们在项目中多次使用到它,但囿于时间关系,一直没仔细探究其背后的原理. 网络上 <word2vec 中的 ...