Android 线性布局属性详解
2015-07-14 18:24
525 查看
线性布局的特点是:各个子元素彼此连接,中间不留空白
线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种,通过android:orientation属性可以设置线性布局的方向。下面是一个简单的线性布局的例子:
线性布局中,有 几个及其重要的参数,直接决定元素的布局和位置,这几个参数是
android:layout_gravity ( 是本元素相对于父元素的对齐方式 )
android:gravity="bottom|right"(是本元素所有子元素的对齐方式,设置在父元素上,多个值用|隔开)
android:layout_gravity (子元素在父元素的对齐方式,设置在子元素上)
当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。
android:padding="10dp" (是本元素所有子元素的与父元素边缘的距离,设置在父元素上)
android:layout_marginLeft="10dp"(子元素与父元素边缘的距离,设置在子元素上)
android:orientation (线性布局以列或行来显示内部子元素)
android:layout_weight ="1"(线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大。
前提是子元素 设置了 android:layout_width = "fill_parent" 属性(水平方向)或 android:layout_height = "fill_parent" 属性(垂直方向)如果某个子元素的 android:layout_width = "wrap_content" 或 android:layout_height
=" wrap_content” ,则 android:layout_weight 的设置值 对该方向上空间的分配刚好相反。
[objc] view
plaincopy
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height = "fill_parent"
android:layout_width = "fill_parent"
android:background = "#AABBCC"
android:orientation= "horizontal"
android:gravity="bottom|right"
>
<TextView android:text = "哈哈"
android:background = "#aa0000"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_gravity="center"
/>
<TextView android:text = "呵呵"
android:background = "#aa0000"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_weight="2"
/>
</LinearLayout >
线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种,通过android:orientation属性可以设置线性布局的方向。下面是一个简单的线性布局的例子:
线性布局中,有 几个及其重要的参数,直接决定元素的布局和位置,这几个参数是
android:layout_gravity ( 是本元素相对于父元素的对齐方式 )
android:gravity="bottom|right"(是本元素所有子元素的对齐方式,设置在父元素上,多个值用|隔开)
android:layout_gravity (子元素在父元素的对齐方式,设置在子元素上)
当 android:orientation="vertical" 时, 只有水平方向的设置才起作用,垂直方向的设置不起作用。即:left,right,center_horizontal 是生效的。
当 android:orientation="horizontal" 时, 只有垂直方向的设置才起作用,水平方向的设置不起作用。即:top,bottom,center_vertical 是生效的。
android:padding="10dp" (是本元素所有子元素的与父元素边缘的距离,设置在父元素上)
android:layout_marginLeft="10dp"(子元素与父元素边缘的距离,设置在子元素上)
android:orientation (线性布局以列或行来显示内部子元素)
android:layout_weight ="1"(线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大。
前提是子元素 设置了 android:layout_width = "fill_parent" 属性(水平方向)或 android:layout_height = "fill_parent" 属性(垂直方向)如果某个子元素的 android:layout_width = "wrap_content" 或 android:layout_height
=" wrap_content” ,则 android:layout_weight 的设置值 对该方向上空间的分配刚好相反。
[objc] view
plaincopy
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_height = "fill_parent"
android:layout_width = "fill_parent"
android:background = "#AABBCC"
android:orientation= "horizontal"
android:gravity="bottom|right"
>
<TextView android:text = "哈哈"
android:background = "#aa0000"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:layout_gravity="center"
/>
<TextView android:text = "呵呵"
android:background = "#aa0000"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:layout_weight="2"
/>
</LinearLayout >
相关文章推荐
- 设计师给了px单位的标注,Android开发到底要设置多少dip、dp、sp?
- Android工程图片资源命名禁忌
- 查看Android数据库文件
- Android利用Fiddler进行网络数据抓包
- Android - LayoutAnimation 动画效果 - 示例
- Android中OptionMenu用法实例
- Android中实现定时器的三种方法
- Android中实现定时器的三种方法 分类: Android 2015-07-14 18:04 11人阅读 评论(0) 收藏
- Android中ContextMenu用法实例
- Android开发:shape和selector和layer-list的(详细说明)
- Android中日期与时间设置控件用法实例
- 使用AlarmManager启动广播、服务、页面(Android定时器)
- 使用AlarmManager启动广播、服务、页面(Android定时器) 分类: Android 2015-07-14 17:49 13人阅读 评论(0) 收藏
- android studio删除Model
- Android Studio 使用笔记:在图形界面使用 Gradle 配置项目所需jar包
- Android Shader渲染器:BitmapShader图片渲染
- Android 开机动画修改
- Android onClick和滑动事件冲突解决
- Android有关JNI 学习(两)为JNI方法名称,数据类型和方法签名的一些知识
- Android studio 查看签名