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. DOM中document对象的常用属性方法

    每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 属性 1  document.anchors  返 ...

  2. AngularJS -- Bootstrap(启动器)

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ Bootstrap(初始化) 这章介绍了Angular的初始化过程,以及如何在必要的时候 ...

  3. Spring中的Service/DAO/DTO

  4. Xcode 上传代码到GitHub

    几乎所有iOS程序员都上过GitHub寻找开源类库,的确,GitHub上有大量优秀的开源类库供大家学习.但是如何在Xcode中上传代码至GitHub呢? (开始之前先安装git,具体方法这里讲的很清楚 ...

  5. Maven在Windows中的配置以及IDE中的项目创建

    Maven在Windows下的配置 1.Maven下载地址:http://maven.apache.org/download.cgi,下载红框里的版本即可. 2.解压到D盘: 3.修改配置文件sett ...

  6. JAVA HashMap 解析

    1.简介(其实是HashMap注释的大致翻译) 本文基于JDK1.8,与JDK1.7中的HashMap有一些区别,看官注意区别. HashMap实现了Map接口,提供了高效的Key-Value访问.H ...

  7. OpenJudge_1321:棋盘问题

    题目描述 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆 ...

  8. hdu 2609 How many 最小表示法

    How many Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. AmCharts 对数据排序后展示

    在官网看到的例子 给chart添加一个排序功能的handler AmCharts.addInitHandler( function(chart){ if (chart.orderByField === ...

  10. PHP多进程编之僵尸进程问题

    上一篇说到了使用pcntl_fork函数可以让PHP实现多进程并发或者异步处理的效果.那么问题是我们产生的进程需要去控制,而不能置之不理.最基本的方式就是fork进程和杀死进程. 通过利用pcntl_ ...