RelativeLayout中子控件常用属性:

子控件默认是从父控件的左上角开始排列的

  • 相对于父控件
 android:layout_alignParentTop="true"     和父控件的顶部对齐android:layout_alignParentBottom="true"  和父控件的底部对齐android:layout_alignParentRight="true"   和父控件的右端对齐android:layout_alignParentLeft="true"    和父控件的左端对齐
  • 相对于给定的ID控件
android:layout_above="@id/cat1"        控件的底部置于给定ID的控件之上
android:layout_below="@id/cat1"        控件底部置于给定ID的控件之下·
android:layout_toRightOf="@id/cat1"    控件的左边缘与给定ID的控件的右边缘对齐
android:layout_toLeftOf="@id/cat1"     控件的右边缘与给定ID的控件的左边缘对齐
android:layout_alignBottom="@id/cat1"  与给定控件的底边缘对齐
android:layout_alignLeft="@id/cat1"    与给定控件的左边缘对齐
android:layout_alignTop="@id/cat1"     与给定控件的定边缘对齐
android:layout_alignRight="@id/cat1"   与给定控件的右边缘对齐
android:layout_alignBaseline="@id/cat1"控件的Baseline与给定ID控件的Baseline对齐,其实这个baseline相当于笔记本里写文字时候的底下的那条线android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
android:layout_toRightOf  在某元素的右边
  • 居中
 android:layout_centerHorizontal="true"    水平居中android:layout_centerVertical="true"      垂直居中android:layout_centerInParent="true"      相对于父控件处在正中央

一个简单的登录界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><RelativeLayoutandroid:layout_height="150dp"android:layout_width="400dp"android:layout_centerInParent="true"android:background="#ff0000"><TextViewandroid:id="@+id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="用户:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/userline"android:layout_marginTop="5dp"android:layout_width="300dp"android:layout_height="40dp"android:layout_toRightOf="@id/user"/><TextViewandroid:id="@+id/passwd"android:layout_marginTop="10dp"android:layout_below="@id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="密码:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/passwdline"android:layout_width="300dp"android:layout_height="40dp"android:layout_below="@id/userline"android:layout_toRightOf="@id/passwd"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="确定"android:layout_alignParentRight="true"android:layout_below="@id/passwdline"/></RelativeLayout>
</RelativeLayout>

所的结果:

布局间的所属关系:

Margin和Padding:

盒模型(控件)主要定义四个区域:内容 (content)、边框距(padding)即内边距、边界(border)和外边距(margin)。 对于初学者,经常会搞不清楚margin,background-color,background- image,padding,content,border之间的层次、关系和相互影响。这里提供一张盒模型的平面示意图,希望便于你的理解和记忆。Margin 是整体移动,带着控件里面的内容(content),而padding 是移动控件里面的内容相对于控件Bprder的距离。

将上述界面进行美化:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><RelativeLayoutandroid:id="@+id/laout1"android:layout_height="150dp"android:layout_width="400dp"android:layout_centerInParent="true"android:background="#ff0000"><TextViewandroid:id="@+id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:text="用户:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/userline"android:layout_marginTop="5dp"android:layout_width="300dp"android:layout_height="40dp"android:layout_toRightOf="@id/user"/><TextViewandroid:id="@+id/passwd"android:layout_marginTop="20dp"android:layout_below="@id/user"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:text="密码:"android:textSize="20dp"android:textColor="#ffffff"/><EditTextandroid:id="@+id/passwdline"android:layout_width="300dp"android:layout_height="40dp"android:layout_below="@id/userline"android:layout_toRightOf="@id/passwd"/><Buttonandroid:id="@+id/btn1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:layout_marginLeft="90dp"android:text="确定"android:layout_below="@id/passwdline"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="取消"android:layout_toRightOf="@id/btn1"android:layout_below="@id/passwdline"android:layout_marginLeft="80dp"/></RelativeLayout><RelativeLayoutandroid:id="@+id/layout2"android:background="#ff0000"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_below="@id/laout1"android:layout_marginTop="10dp"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="100dp"android:src="@drawable/picture2"/></RelativeLayout>
</RelativeLayout>

如下图所示:

安卓按键快速美化:

  • 在res/drawable目录下新建按钮样式文件 btn_normal.xml(正常状态) 和 btn_pressed.xml(按下状态)。

btn_normal.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!-- 圆角的半径 --><corners android:radius="10dp"/><!-- 填充颜色 --><solid android:color="#3a8fea"/>
</shape>

btn_pressed.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!-- 圆角的半径 --><corners android:radius="10dp"/><!-- 填充颜色 --><solid android:color="#0662f5"/>
</shape>
  • 在res/drawable目录下新建样式文件 btn_selector.xml 文件,定义按钮的不同状态样式。

btn_selector.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><!-- 正常状态 --><item android:drawable="@drawable/btn_normal" android:state_pressed="false"/><!-- 按下状态 --><item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/></selector>

练习制作刷卡界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><ImageView android:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"android:src="@drawable/pic_rf"/><ImageViewandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerInParent="true"android:src="@drawable/card"android:paddingLeft="100dp"android:paddingTop="50dp"/><Buttonandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:layout_centerHorizontal="true"android:layout_marginBottom="30dp"android:layout_alignParentBottom="true"android:text="刷卡"android:background="@drawable/btn_selector"android:textColor="#ff0000"/><RelativeLayoutandroid:layout_height="wrap_content"android:layout_width="match_parent"android:background="#00ff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30dp"android:text="刷卡界面"android:layout_marginLeft="20dp"/><Buttonandroid:id="@+id/butn1"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="注册"android:layout_alignParentRight="true"     /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查询信息"android:layout_toLeftOf="@id/butn1"android:layout_marginRight="20dp"/></RelativeLayout></RelativeLayout>

结果如下:

LinearLayout常用属性(它的父控件还是RelativeLayout,所以RelativeLayout的属性还可以拿来用):

  • orientation: 布局中控件的排列方式,有vertical(竖直,默认)horizontal(水平)两种方式
  • gravity:控制组件所包含的子元素的对齐方式,可多个组合,如left|buttom(这个是基础控件相对于父控件来说的)
  • layout_gravity: 控制该组件在父容器中的对齐方式,(这个是布局控件相对于父控件来说的)
  • layout_width:布局宽度,通常不直接写数字的,用wrap_content(组件实际大小)fill_parent或者match_marent(填满父容器)
  • layout_height:布局的高度,参数同上
  • background:为组件设置一个背景图片,或者直接用颜色覆盖

Weight(权重):

该属性用来等比例地划分区域。

  • 最简单的用法:要等比例划分,分谁,谁为0,Weight按比例即可
  • 当我们设置宽度为0dp时,同时设置weight属性为1,意思就是在宽度方向上所占比重为1。如果将height设置为0,同时设置weight属性为2,意思就是在竖直方向所占比重为2。

divider分割线:

该属性用于LinearLayout设置分割图片,通过showDivider来设置分割线的所在位置,有四个可选值:nonemiddlebeginingend,当然还可以通过:

  • divider: 为LinearLayout设置分割线的图片
  • showDivider: 设置分割线所在位置,有四个通道:nonemiddlebeginingend
  • dividerPadding: 设置分割线的Padding

设置分割线(divider):


然后编辑该分割线的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">      shape是分割线的形状<sizeandroid:width="200dp"  分割线的宽android:height="2dp"   分割线的高/><strokeandroid:color="#000000"  这个是分割线的颜色/>
</shape>

使用线性布局和相对布局写一个丑陋的登录界面:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg_shopping_menu"tools:context=".MainActivity" ><LinearLayoutandroid:layout_width="300dp"android:layout_height="wrap_content"android:layout_centerInParent="true"android:orientation="horizontal" ><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:divider="@drawable/divider"android:showDividers="middle|end"android:dividerPadding="2dp"android:orientation="vertical" ><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="账号:" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="密码:" /><TextViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:gravity="center"android:textStyle="bold"android:textSize="15dp"android:text="ID:" /></LinearLayout><LinearLayoutandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="5"android:orientation="vertical" ><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><EditTextandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /></LinearLayout></LinearLayout></RelativeLayout>

结果如下图所示:

Android相对布局(RelativeLayout)常用属性、练习使用按键、文本框等控件、线性布局(LinearLayout)属性相关推荐

  1. python时间控件readonly属性_Selenium2+python自动化----js处理日历控件(修改readonly属性)...

    前言 日历控件是web网站上经常会遇到的一个场景,有些输入框是可以直接输入日期的,有些不能,以我们经常抢票的12306网站为例,详细讲解如何解决日历控件为readonly属性的问题. 基本思路:先用j ...

  2. VC++静态文本框/PICTURE控件的notify属性

    RT,该属性对应的是SS_NOTIFY,但是很多人误以为是WM_NOTIFY .该属性可以用ModifyStyle函数修改.

  3. Android:相对布局RelativeLayout常用属性

                    RelativeLayout中子控件常用属性: 1.相对于父控件,例如:android:layout_alignParentTop="true"   ...

  4. Android Studio 线性布局Linearlayout的控件位置控制l属性Layout_margin失效问题解决

    Android Studio 线性布局Linearlayout的控件位置控制l属性Layout_margin失效问题解决 问题:如layout_marginRight ="50dp" ...

  5. Android UI控件和布局

    说明: 本文是郭霖<第一行代码-第3版>的读书笔记 4.1 如何编写程序界面 编写XML,这是传统的方法 ConstraintLayout,Google推出的新方法,可以在可视化编辑器中拖 ...

  6. Android 手机卫士--自定义组合控件构件布局结构

    由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...

  7. Android 通过代码改变控件的布局方式

    在很多情况下当我们在xml中布局的方式并不能满足我们的要求,而这时我们就需要通过在代码中控制控件的布局 根据不同的条件来控制布局.首先来了解一下安卓中的一些单位 dip: device indepen ...

  8. android的属性如何使用方法,Android第二大控件,EditText的属性和使用方法

    原标题:Android第二大控件,EditText的属性和使用方法 EditText与TextView非常相似,它甚至与TextView 共用了绝大部分XML属性和方法.EditText与TextVi ...

  9. WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系

    WPF布局控件与子控件的HorizontalAlignment/VerticalAlignment属性之间的关系: 1.Canvas/WrapPanel控件: 其子控件的HorizontalAlign ...

最新文章

  1. PyQt 编程基本思想-HelloWorld
  2. codelite13 wxWidgets3 macos开发环境配置
  3. 【译】Tutorials ---- Hello, World
  4. 拨盘Demo大赛,获奖公布-20170710
  5. java hive配置_Hive配置项的含义详解(1)
  6. linux io阻塞问题
  7. 【跃迁之路】【712天】程序员高效学习方法论探索系列(实验阶段469-2019.2.2)...
  8. python网站设计中接口的作用_python接口自动化(三)--如何设计接口测试用例(详解)...
  9. mysql修改表编码为ytf8_如何修复“TypeError:x和y必须具有相同的数据类型,got”tf.uint8型!= tf.float32型“当我试图以十为单位调整图像大小时...
  10. python测试系列教程 —— 调试日志logging
  11. python爬虫之ip代理参数/动态加载数据抓取
  12. Vue配置TinyMCE富文本编辑器 + 图片(本地)上传到服务器
  13. 计算机音乐演奏jojo,【FF14】诗人演奏用 il vento d'oro(动画《JOJO的奇妙冒险 黄金之风》插曲)...
  14. 数据库mysql从入门到放弃
  15. 物联网嵌入式系统开发应用软件公司怎么选择
  16. 想转行学IT,到底要不要去培训机构?
  17. java农历万年历课程设计_Java编写多功能万年历程序的实例分享
  18. uni-app实现一键登录
  19. Footprint:如何寻找有增长潜力的NFT项目?
  20. #EDA(电子设计自动化)

热门文章

  1. 【TensorFlow】——实现minist数据集分类的前向传播(常规神经网络非卷积神经网络)
  2. 【TensorFlow】——索引与切片
  3. mysql select array_从数据库select查询出来的数组
  4. 计算机如何打开无线网络适配器,win7系统下网络适配器打不开怎么解决
  5. Pytorch 自定义激活函数前向与反向传播 ReLu系列 含优点与缺点
  6. python语言打印菱形_Python打印菱形
  7. uniaccess进程无法结束 拒绝访问_嵌入式Linux编程——程序员小白不懂的进程、信号量、并发、互斥...
  8. 计算机发展趋势是规格化,2016年春季计算机应用基础月考卷(4月).doc
  9. 56、servlet3.0-与SpringMVC整合分析
  10. 不能读取文件“itunes.library.itl”因为它是由更高级别的itunes所创建的