android layout组件,Android UI学习 - Linear Layout, RelativeLayout
1.一些常用的公共属性介绍
1) layout_width -宽
fill_parent: 宽度和父元素相同,wrap_content: 宽度随本身的内容所调整,或者指定 px值来设置宽
2) layout_height - 高
fill_parent: 高度和父元素相同,wrap_content: 高度随本身的内容所调整,或者指定 px值来设置高
3) background -设置背景图
4) padding -设置边距
可以具体设置paddingBottom,paddingLeft,paddingRight,paddingTop来设定不同的px值
5) id -该object的id号
@+id/id1 代表添加新的id名为id1, @id/id1 代表引用id1的控件
6) layout_weight -重要度
个人理解为显示的优先级。默认为0(最高),数值越大,优先级越低!参考下面的Linear Layout例子。要让layout_weight生效,需要父层或父父层的相应layout_width/layout_height = "fill_parent",否则wrap_content会压缩到最小足够空间!
7) layout_gravity- Container组件的对齐方式
组件在layout里面的对齐方式。
8) gravity-文字在组件里的对齐方式
例如设置button里面的文字在button中居中显示。
* 大多数属性是可以调用对应的函数来动态改变状态的,请查看SDK Doc。
2. Linear Layout 线形布局
orientation -容器内元素的排列方式。vertical: 子元素们垂直排列,horizontal: 子元素们水平排列。在代码里可通过setOrientation()进行动态改变,值分别为HORIZONTAL或者VERTICAL。
*在Linear Layout, 宽度/高度都是按着组件的次序逐个占用的!所以当某个组件设置"fill_parent",在没有设置Layout_weight的情况下,该组件会占用了余下的空间,那么在它后面的组件就会显示不出来。如下图的EditText如果没有设置android:layout_weight="1",它下面的其他组件就看不见了!
baselineAligned 一般情况下,这个属性默认为true,代表在同一方向的组件都基于第一个组件对齐。所以可以看到下图的text1, button1, text2是在同一水平线的。当不需要这效果时,可以设置为false。
xml代码:
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:text="@string/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/edittext"
/>
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:text="text1"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
android:text="text2"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="buttom"
/>
3. RelativeLayout 相对定位布局
这个布局比较易懂,但组件间容易存在依赖关系,“牵一发而动全身“,所以在确定组件间布局关系不会再变动时,可以考虑采用!先看看xml代码:
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/relativelayout">
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_toRightOf="@id/image"
/>
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="button1"
android:layout_toRightOf="@id/image"
android:layout_below="@id/text1"
/>
Java代码(动态添加组件):
public class RelativeDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relative);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, //width
ViewGroup.LayoutParams.WRAP_CONTENT //height
);
//设置editText layout_below="@id/button1"
lp.addRule(RelativeLayout.BELOW, R.id.button1);
//设置editText layout_alignLeft="@id/image"
lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.image);
((RelativeLayout) findViewById(R.id.relativelayout)).addView(
new EditText(this), lp);
}
}
效果图:
先添加参照物(ImageView),然后就可以依次添加其他组件,定义位置规则rule!位置规则是不分先后的!另外ADT插件提供的预览图,有时候是不准的,未能及时更新,所以最好还是要到模拟器上测试!
RelativeLayout的xml属性很多,总的来说分为2类:
1)要指定参照物的,layout_alignBottom,layout_toLeftOf,layout_above,layout_alignBaseline系列的;
layout_above = ”@id/text1“
2)以parent为参照物,设置true/false,layout_centerVertical,layout_alignParentLeft系列的。
layout_alignParentLeft = ”true“
其中layout_alignWithParentIfMissing是比较有用且要注意的属性,当设置为true,在指定的参照物找不到的情况下,会使用parent作为新的参照物!
RelativeLayout.LayoutParams是用于设置位置规则的。上述的xml属性均来自此静态类。但它的AddRule(int verb, int anchor),参数的verb动作却是引用RelativeLayout的常量,而这些常量和部分xml属性相对应。参数anchor的值可能是参照物的id,RelativeLayout.TRUE,-1(当不需要指定anchor的verb)。可以这样理解verb和anchor:
xml属性(verb) = "value"(anchor)
其中它的构造函数之一: RelativeLayout.LayoutParams(int w, int h),参数指定所要设置子View的宽度和高度。
android layout组件,Android UI学习 - Linear Layout, RelativeLayout相关推荐
- Android UI学习 - Linear Layout, RelativeLayout
1. 一些常用的公共属性介绍 1) layout_width - 宽 fill_parent: 宽度和父元素相同,wrap_content: 宽度随本身的内容所调整,或者指定 px 值来设置宽 2) ...
- android adapter 组件,Android UI - AdapterView 及其子类
AdapterView AdapterView 是一个抽象类,其派生的子类在用法上十分相似: AdapterView 继承了 ViewGroup: AdapterView 及其子类的继承关系如下: A ...
- Android四大组件之——Broadcast学习总结
1.Broadcast概念 是一个全局的监听器,属于Android四大组件之一.Broadcast(广播)是一种广泛运用的在应用程序(APP)之间传输信息的机制.而BroadcastReceiver( ...
- android 模糊组件,Android实现局部模糊效果
本文实例为大家分享了Android实现局部模糊效果展示的具体代码,供大家参考,具体内容如下 要实现模糊或者毛玻璃效果,使用PS自然最方便(模糊的区域就较为固定): 也可在代码里进行动态处理. 因为要模 ...
- android高级组件,Android高级组件ImageSwitcher图像切换器使用方法详解
图像切换器(ImageSwitcher),用于实现类似于Windows操作系统的"Windows照片查看器"中的上一张.下一张切换图片的功能.在使用ImageSwitcher时,必 ...
- android date 组件,Android中TimePicker与DatePicker时间日期选择组件的使用实例
效果和代码都非常直观: 实例1:TimePicker xmlns:tools="http://schemas.android.com/tools" android:layout_w ...
- android缩放组件,Android控件实现图片缩放功能
1 简介 先来一张效果图 TIM图片.gif 上图中灰色的一块是ImageView控件,ImageView中的图片进行左右上下移动,以及双指缩放. 对于android控件的缩放移动,点这里----an ...
- 3 描述android的组件,Android基础------Intent组件
1.什么是intent 同Activity一样,也是Android应用组件 在Android中承担着一种指令输出的作用 Intent负责对应用中一次操作的动作及动作相关的数据进行描述. Android ...
- android 抽屉组件,Android组件之DrawerLayout实现抽屉菜单
DrawerLayout组件同样是V4包中的组件,也是直接继承于ViewGroup类,所以这个类也是一个容器类. 抽屉菜单的摆放和布局通过android:layout_gravity属性来控制,可选值 ...
最新文章
- SAP BW 学习笔记(一)
- Eigen教程1 - 基础
- silverlight中的socket编程注意事项
- 《MongoDB权威指南》读书笔记 第一章 简介
- 通信基站电源维护培训PPT课件
- 二级c语言作答文件不存在,全国计算机等级考试二级C语言上机考试题库及答案...
- Django QuerySet API文档
- Music List
- 操作系统中的死锁问题
- C#图解教程读书笔记(深入理解类)
- 面试题 02.01. 移除重复节点(链表删除操作模板)
- Kafka 分布式消息系统 基础概念剖析
- VC连接SQL2005
- python数据分析
- python中使用matplotlib.pyplot画函数图像
- 微信小程序上传文件报错: errMsg: “uploadFile:fail createUploadTask:fail invalid url“
- 真的无语,MSRA连国防七子及北邮学生都不招了
- 3.6.3 Cache和主存的映射方式
- 高性能MySQL(第四版):一、MySQL架构
- 晶振旁外接电容的选择