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. 微服务~Eureka实现的服务注册与发现及服务之间的调用

    微服务里一个重要的概念就是服务注册与发现技术,当你有一个新的服务运行后,我们的服务中心可以感知你,然后把加添加到服务列表里,然后当你死掉后,会从服务中心把你移除,而你作为一个服务,对其它服务公开的只是 ...

  2. foreach循环中为什么不要进行remove/add操作

    先来看一段代码,摘自阿里巴巴的java开发手册 List<String> a = new ArrayList<String>(); a.add("1"); ...

  3. jdk版本查看,以及java -version 和JAVA_HOME不一致问题

    一.jdk版本查看及位数查看 在cmd进入命令行窗口,输入java -version 可以查看安装的jdk版本,如图: 当有64-bit时代表是64位jdk,如果没有则默认是32位的. 二.java ...

  4. Laravel5 控制器

    Request 一.取值 1.取值 echo $request->input('name','这是默认值'); 2.取得所有值 $array=$request->all(); 3.判断值是 ...

  5. 项目管理软件之争,禅道和JIRA大对比

    本文摘要: 一. 产品介绍 二. 界面设计 1. 界面颜色设计 2. 布局结构 三. 功能区别 四. 价格对比 五. 后期服务 六. 优缺点 七. 总结 说到项目管理软件,不得不提的是禅道和JIRA. ...

  6. Log4net日志使用教程-控制台、文本、数据库三种记录方式

    一.log4net简介: 1. Log4net的优点: 几乎所有的大型应用都会有自己的用于跟踪调试的API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而一个管理员可能需要有一套强大的 ...

  7. zoj2818 Root of the Problem 简单数学 开方

    Root of the Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB Given positive integers B and ...

  8. C#微信公众号/订阅号开发 接口源码

    using System; using System.Web; using System.IO; using System.Text; using System.Web.Security; using ...

  9. BZOJ-4915-简单的数字题

    Description 对任意的四个不同的正整数组成的集合A={a_1,a_2,a_3,a_4 },记S_A=a_1+a_2+a_3+a_4,设n_A是满足a_i+a_j (1 ≤i<j≤4)| ...

  10. nodejs+express-实现文件上传下载管理的网站

    Nodejs+Express-实现文件上传下载管理的网站 项目Github地址(对你有帮助记得给星哟):https://github.com/qcer/updo 后端:基于nodejs的express ...