Android 开发笔记___RelativeLayout
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的更多相关文章
- Android开发笔记:打包数据库
对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...
- Android开发笔记--hello world 和目录结构
原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...
- 【转】Android开发笔记(序)写在前面的目录
原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...
- [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明
接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...
- [APP] Android 开发笔记 002-命令行创建默认项目结构说明
接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.
- Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计
Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...
- Android开发笔记(一百三十四)协调布局CoordinatorLayout
协调布局CoordinatorLayout Android自5.0之后对UI做了较大的提升.一个重大的改进是推出了MaterialDesign库,而该库的基础即为协调布局CoordinatorLayo ...
- 【转】Android开发笔记——圆角和边框们
原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...
- 《ArcGIS Runtime SDK for Android开发笔记》
开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...
随机推荐
- C#设计模式之四抽象工厂模式(AbstractFactory)【创建型】
一.引言 写了3篇有关设计模式的文章了,大家有了些反馈,说能从中学到一些东西,我感到很欣慰,那就继续努力.今天我要写第四个模式了,该模式叫抽象工厂.上一篇文章我们讲了[工厂方法]模式,它是为了 ...
- android studio集成ijkplayer
介绍 ijkplayer是一款非常火的开源视频播放器,android和IOS通用.关于怎么编译怎么导入android Studio中自己的项目,其中坑很多,本篇记录下自己的操作记录.ijkplayer ...
- js如何判断一个对象为空
今天碰到一个问题如何判断一个对象为空? 总结的方法如下: 1.使用jquery自带的$.isEmptyObject()函数. var data={}; console.log($.isEmptyObj ...
- 【运维】CPU负载
最近对我的本本(4核8线程)用top命令看系统状况出现了CPU利用率超过200%的情况,非常诧异,查了下相关资料,把这个问题弄清楚了.首先来分析下CPU Load load average: 0.09 ...
- 使用LayUI操作数据表格
接着 上一篇 继续完善我们的demo,这次我们加一个搜索按钮 搜索 在table标签的上方,加入这样一组html <div class="demoTable"> 搜索商 ...
- 支持向量机SVM(一)
[转载请注明出处]http://www.cnblogs.com/jerrylead 1 简介 支持向量机基本上是最好的有监督学习算法了.最开始接触SVM是去年暑假的时候,老师要求交<统计学习理论 ...
- Java Socket通信以及可能出现的问题解决
Java中基于TCP协议实现网络通信的两个类:客户端的Socket和服务器端的ServerSocket. Socket通信模型如图所示: 不管Socket通信的功能有多复杂,任何socket通信过程的 ...
- postman - 基本操作
设置环境 collections 导入 runner 导入 api 配置api 编写测试(请求和响应)脚本 api的保存和导出 setting file -- setting
- ubuntu系统如何屏幕截图
我们知道,windows下有很多截图的软件和插件,那么在ubuntu系统下我们该怎样截图呢? 下面就让小编来告诉你几种简单的方法吧. 工具/原料 ubuntu系统电脑 方法一: 1.也许很多朋友都知道 ...
- Quartz.NET实现作业调度
一.Quartz.NET介绍 Quartz.NET是一个强大.开源.轻量的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,用C#改写,可用于winform和asp ...