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

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" >
	
	<RelativeLayout
	    android:layout_height="150dp"
	    android:layout_width="400dp"
	    android:layout_centerInParent="true"
	    android:background="#ff0000">
	    <TextView
	        android: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"
	    />
	    <EditText
	        android:id="@+id/userline"
	        android:layout_marginTop="5dp"
	        android:layout_width="300dp"
	        android:layout_height="40dp"
	        android:layout_toRightOf="@id/user"/>
	    <TextView
	        android: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"
	    />
	    <EditText
	        android:id="@+id/passwdline"
	        android:layout_width="300dp"
	        android:layout_height="40dp"
	        android:layout_below="@id/userline"
	        android:layout_toRightOf="@id/passwd"/>
	    <Button
	        android: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" >
	
	<RelativeLayout
	    android:id="@+id/laout1"
	    android:layout_height="150dp"
	    android:layout_width="400dp"
	    android:layout_centerInParent="true"
	    android:background="#ff0000">
	    <TextView
	        android: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"
	    />
	    <EditText
	        android:id="@+id/userline"
	        android:layout_marginTop="5dp"
	        android:layout_width="300dp"
	        android:layout_height="40dp"
	        android:layout_toRightOf="@id/user"/>
	    <TextView
	        android: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"
	    />
	    <EditText
	        android:id="@+id/passwdline"
	        android:layout_width="300dp"
	        android:layout_height="40dp"
	        android:layout_below="@id/userline"
	        android:layout_toRightOf="@id/passwd"/>
	    <Button
	        android: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"
	        />
	    <Button
	        android: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>
	<RelativeLayout
	    android: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"
	    >
	    <ImageView
	        android: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"?>
<shape
    xmlns: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"/>
	<ImageView
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:layout_centerInParent="true"
	    android:src="@drawable/card"
	    android:paddingLeft="100dp"
	    android:paddingTop="50dp"
	    />
	<Button
	    android: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"
	    />
	<RelativeLayout
	    android:layout_height="wrap_content"
	    android:layout_width="match_parent"
	    android:background="#00ff00"
	    >
	 <TextView
	     android:layout_width="wrap_content"
	     android:layout_height="wrap_content"
	     android:textSize="30dp"
	     android:text="刷卡界面"
	     android:layout_marginLeft="20dp"
	     />
	 <Button
	     android:id="@+id/butn1"
	     android:layout_height="wrap_content"
	     android:layout_width="wrap_content"
	     android:text="注册"
	     android:layout_alignParentRight="true"     
	     />
	 <Button
	     android: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是分割线的形状
    <size
        android:width="200dp"  分割线的宽
        android:height="2dp"   分割线的高
        />
    <stroke
        android: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" >

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="horizontal" >

        <LinearLayout
            android: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" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="15dp"
                android:text="账号:" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="15dp"
                android:text="密码:" />

            <TextView
                android: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>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:orientation="vertical" >

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

结果如下图所示:
在这里插入图片描述

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android从入门到精通》 第1章 初识Android 1.1 什么是Android 1.1.1 移动信息设备分类 1.1.2 Open Handset Alliance和 Android 1.2 Android简介 1.2.1 Andriod的历史 1.2.2 Andriod的未来 1.2.3 Andriod平台的技术架构 1.3 Android应用程序构成 1.3.1 活动(Activity) 1.3.2 意图(Intent) 1.3.3 服务(Service) 1.3.4 内容提供器(ContentProvider) 1.4 Android网上资源 第2章 搭建Android开发环境 2.1 Android开发环境要求 2.2 JDK的安装和配置 2.2.1 安装JDK 2.2.2 配置JDK 2.3 Android SDK的下载和安装 2.3.1 下载Android SDK 2.3.2 安装Android SDK 2.3.3 创建Android虚拟设备 2.4 Eclipse的下载和安装 2.4.1 下载和安装Eclipse 2.4.2 安装和配置Eclipse中Android插件 2.5 使用Eclipse开发Android应用程序 2.5.1 使用Eclipse创建Android项目 2.5.2 Eclipse中Android项目架构 2.5.3 Eclipse中Android项目的调试和运行 第3章 Android中的Activity 3.1 Activity的作用 3.2 单Activity的Android应用 3.2.1 Activity的生命周期 3.2.2 Activity类的结构 3.3 Activity的两种界面设计方式 3.3.1 基于XML的界面设计 3.3.2 基于代码的界面设计 3.4 应用实例:在界面中显示图片 第4章 Android人机界面和常用组件 4.1 用户人机界面元素分类 4.1.1 视图组件(View) 4.1.2 视图容器组件(ViewGroup) 4.1.3 布局组件(Layout) 4.1.4 布局参数(LayoutParams) 4.2 常用Widget组件 4.2.1 文本框视图(TextView) 4.2.2 按钮(Button) 4.2.3 图片按钮(ImageButton) 4.2.4 编辑框(EditText) 4.2.5 多项选择(CheckBox) 4.2.6 单项选择(RadioGroup) 4.2.7 下拉列表(Spinner) 4.2.8 自动完成文本框视图(AutoCompleteTextView) 4.2.9 日期选择器(DatePicker) 4.2.10 时间选择器(TimePicker) 4.2.11 数字时钟(DigitalClock) 4.2.12 表状时钟(AnalogClock) 4.2.13 进度条(ProgressBar) 4.2.14 拖动条(SeekBar) 4.2.15 评分条(RatingBar) 第5章 Android中的视图组件 5.1 视图组件 5.1.1 图片视图(ImageView) 5.1.2 滚动视图(ScrollView) 5.1.3 网格视图(GridView) 5.1.4 列表视图(ListView) 5.1.5 切换图片(ImageSwitcher和Gallery) 5.1.6 标签切换(Tab) 5.2 通用XML属性 第6章 Android菜单和布局设计 6.1 菜单(Menu) 6.1.1 上下文菜单(ContextMenu) 6.1.2 选项菜单(OptionsMenu) 6.1.3 基于XML的菜单结构 6.2 界面布局设计 6.2.1 基于XML的布局设计 6.2.2 线性布局LinearLayout) 6.2.3 相对布局RelativeLayout) 6.2.4 表格布局(TableLayout) 6.2.5 绝对布局(AbsoluteLayout) 6.3 界面中的字体 6.3.1 设置系统字体 6.3.2 引用用户自定义字体 6.4 应用实例详解:制作手机桌面 6.4.1 实例分析 6.4.2 实例实现 第7章 Android中的核心Intent 7.1 Intent的作用 7.1.1 多Activity的Android应用 7.1.2 Activity之间的消息传递 7.2 Intent的分类 7.2.1 Action Intent 7.2.2 Broadcast Intent 7.3 解析Intent的实现 7.3.1 Intent Receiver 7.3.2 Intent Filter 7.4 设置Activity许可 7.5 应用实例详解:电话拨号程序 7.5.1 实例分析 7.5.2 实例实现 第8章 Android中的后台服务Service 8.1 Service的作用 8.2 Service的实现 8.2.1 创建Service 8.2.2 启动Service 8.3 Toast和Notification应用 8.3.1 使用Notification通知 用户服务启动 8.3.2 使用Toast显示通知信息 8.4 应用实例详解:播放背景音乐 8.4.1 实例分析 8.4.2 实例实现 第9章 Android中的数据存储 9.1 使用Preferences存储数据 9.1.1 访问Preferences的API 9.1.2 使用XML存储Preferences数据 9.2 使用文件存储数据 9.2.1 访问应用中的文件数据 9.2.2 访问设备中独立的文件数据 9.3 使用SQLite数据库存储数据 9.3.1 SQLite数据库简介 9.3.2 SQLite数据库操作 9.3.3 使用SQLiteDatabase对象操作数据库 9.3.4 Cursor的使用 9.4 使用ContentProvider 9.4.1 定义ContentProvider 9.4.2 使用ContentProvider进行CRUD操作 9.5 应用实例详解:创建音乐 播放列表 9.5.1 实例分析 9.5.2 实例实现 第10章 Android的网络通信 10.1 访问Internet 10.1.1 使用WebKit组件访问Internet 10.1.2 使用Apache HttpComponents访问Internet 10.2 Socket通信 10.3 应用实例详解:手机 短信程序 10.3.1 实例分析 10.3.2 实例实现 第11章 Android的GPS应用开发 11.1 GPS在手机中的应用 11.2 Android Location-Based API简介 11.3 Android模拟器支持的GPS定位文件 11.3.1 KML 11.3.2 NMEA 11.4 应用实例详解:确定当前 位置的GPS程序 11.4.1 实例分析 11.4.2 实例实现 11.5 基于Google Map的应用 11.5.1 使用MapView显示地图 11.5.2 使用MapController控制地图缩放 11.6 应用实例详解:普通地图和 卫星地图切换 11.6.1 实例分析 11.6.2 实例实现 第12章 Android的搜索引擎和Gtalk开发 12.1 搜索引擎在手机中的应用 12.1.1 本地搜索 12.1.2 Web搜索 12.2 Android搜索引擎API简介 12.3 应用实例详解:过滤式搜索 引擎程序 12.3.1 实例分析 12.3.2 实例实现 12.4 Android的GTalk应用开发 12.4.1 GTalk在手机中的应用 12.4.2 Android GTalk API简介 12.4.3 为GTalk配置Android模拟器 12.5 应用实例详解:Google GTalk 程序 12.5.1 实例分析 12.5.2 实例实现 第13章 Android综合案例开发:俄罗斯方块 13.1 俄罗斯方块游戏功能需求 13.2 俄罗斯方块游戏UI设计 13.3 俄罗斯方块游戏功能实现 13.4 俄罗斯方块游戏演示

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值