Android 线性布局属性及详解
2015-11-19 19:43
561 查看
线性布局的特点是:各个子元素彼此连接,中间不留空白
线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种,通过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 >
相关文章推荐
- Android studio 所有方法变红色
- Gradle学习手册
- android Services注意地方
- Android中的visibility属性
- android触控之MotionEvent
- android windows的一些item属性
- 关于Activity的横竖屏切换引起的生命周期变化的问题
- AsyncTask源码浅析
- [转]Android关于使用混淆文件proguard-project.txt笔记
- Android Studio1.4.x JNI开发基础-基本环境配置
- Android中ListView分类
- Otto与EventBus框架比较
- Android——适配器
- android:修改PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色
- 【Android】安卓布局文件中xmlns属性
- Android Launcher3主菜单背景改为黑色
- com.android.dex.DexException: Multiple dex files define的解决办法 (Android Studio)
- Android-图片的缓存实现笔记
- android 4.4以下loadIocn nullpointerException问题
- 一、dialog.show()引起的android.view.WindowManager$BadTokenException错误