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. windows 2008 R2 断电重启进入修复模式

    windows 2008 R2 意外断电重启进入修复模式 现在出现一个问题,就是当机房停电的时候,计算机自动进入到修复模式,当人不在机房的时候,容易造成服务器无法访问,我相信正常启动应该没什么问题吧, ...

  2. 实例化vue之前赋值html元素导致事件失效

    先实例化Vue对象,再操作其他对象,Vue对象声明时会渲染html容器内的所有元素, 会导致元素事件失效或dom元素重新创建,所以涉及html元素的对象都要在实例化Vue之后执行. 下面是简要的例子, ...

  3. Win10下python3和python2同时安装并解决pip共存问题

    特别说明,本文是在Windows64位系统下进行的,32位系统请下载相应版本的安装包,安装方法类似. 使用python开发,环境有Python2和 python3 两种,有时候需要两种环境切换使用,下 ...

  4. C语言 实现base64

    #include <stdio.h> const char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw ...

  5. win10 UWP 隐式转换

    implicit operator string <script type="text/javascript"> $(function () { $('pre.pret ...

  6. nexus3 添加第三方本地文件jar到仓库

    因为nexus3和nexus2手动上传第三方jar有点区别 故记录一下. 如上传京东 open-api-sdk-2.0.jar 首先创建一个目录 方便执行上传的时候url参数 也可以不创建 mkdir ...

  7. iOS开发中使用文字图标iconfont

    在iOS的开发中,各种图标的使用是不可避免的,如果把全部图标做成图片放在项目中,那么随着项目的逐渐庞大起来,图片所占的地方就会越来越大,安装包也就随之变大了,如果图标需要根据不同的场景改变使用不同的颜 ...

  8. 【转】ATA Secure Erase

    ATA Secure Erase     This procedure describes how to use the hdparm command to issue a Secure Erase  ...

  9. [Java Web 第一个项目]客户关系处理系统(CRM)项目总结

    一.table的应用: 1.表格的常用属性 基本属性有:width(宽度).height(高度).border(边框值).cellspacing(表格的内宽,即表格与tr之间的间隔).cellpadd ...

  10. mybatis mysql 批量插入

    场景描述: 使用mybatis操作mysql数据库,进行批量插入数据,提高代码质量和执行效率. 环境: mybatis spring mysql java xml配置文件 <insert id ...