Android编程之线性布局LinearLayout实例简析

本文实例讲述了Android编程之线性布局LinearLayout用法。分享给大家供大家参考,具体如下:

线性布局(LinearLayout)

可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列)。

下面示例是在别人基础上修改的main.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:layout_weight="1" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="@string/color_green" android:textColor="#ff0000" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="@string/color_blue" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="@string/color_black" android:background="#000000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="@string/color_yellow" android:background="#aaaa00" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="@string/color_unknown" android:background="#00aaaa" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2"> <TextView android:text="@string/color_red" android:gravity="fill_vertical" android:background="#aa0000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2"/> <TextView android:text="@string/color_white" android:textColor="#ff0000" android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2"/> </LinearLayout> </LinearLayout>

string.xml

<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, TestAbsoluteLayoutActivity!</string> <string name="app_name">TestAbsoluteLayout</string> <string name= "color_red">red</string> <string name= "color_green">green</string> <string name= "color_blue">blue</string> <string name= "color_white">white</string> <string name= "color_black">black</string> <string name= "color_yellow">yellow</string> <string name= "color_unknown">unknown</string> </resources>

效果图如下:

常用的属性:

android:orientation:可以设置布局的方向
android:gravity:用来控制组件的对齐方式
layout_weight:控制各个组件在布局中的相对大小

更多关于Android编程布局相关内容可查看本站专题:《Android布局layout技巧总结》

希望本文所述对大家Android程序设计有所帮助。

时间: 2024-08-10 15:36:22

Android编程之线性布局LinearLayout实例简析的相关文章

Android编程之线性布局LinearLayout实例简析_Android

本文实例讲述了Android编程之线性布局LinearLayout用法.分享给大家供大家参考,具体如下: 线性布局(LinearLayout) 可以让它的子元素垂直或水平的方式排成一行(不设置方向的时候默认按照垂直方向排列). 下面示例是在别人基础上修改的main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.

android 线性布局LinearLayout实例代码_Android

布局文件:res/layout/activity_my.xml 复制代码 代码如下: [html]  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/LinearLayout"     android:

android 线性布局LinearLayout实例代码

布局文件:res/layout/activity_my.xml复制代码 代码如下:[html]  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/LinearLayout"     android:la

Android复选框对话框用法实例简析_Android

本文实例分析了Android复选框对话框用法.分享给大家供大家参考,具体如下: /** * 创建筛选复选框对话框 * @param guoguanglist 联赛名 * @param flags 是否选择 * @create_time 2011-10-26 下午3:59:54 */ private void initFilterDialog(String[] iNamelist, final boolean[] iFlags) { Builder builder = new android.ap

Android复选框对话框用法实例简析

本文实例分析了Android复选框对话框用法.分享给大家供大家参考,具体如下: /** * 创建筛选复选框对话框 * @param guoguanglist 联赛名 * @param flags 是否选择 * @create_time 2011-10-26 下午3:59:54 */ private void initFilterDialog(String[] iNamelist, final boolean[] iFlags) { Builder builder = new android.ap

android开发之listView组件用法实例简析

本文实例讲述了android开发之listView组件用法.分享给大家供大家参考,具体如下: 关于Android ListView组件中android:drawSelectorOnTop含义 android:drawSelectorOnTop="true"  点击某一条记录,颜色会显示在最上面,记录上的文字被遮住,所以点击文字不放,文字就看不到. android:drawSelectorOnTop="false" 点击某条记录不放,颜色会在记录的后面,成为背景色,但

Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解_Android

本文实例分析了Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout.分享给大家供大家参考,具体如下:  一.绝对布局AbsoluteLayout 绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差. 下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:l

Android学习笔记(11):线性布局LinearLayout

线性布局LinearLayout是指在横向或是竖向一个接一个地排列,当排列的组件超出屏幕后,超出的组件将不会再显示出来. LinearLayout支持的XML属性和对应方法如表所示: Attribute Name Related Method Description android:baselineAligned setBaselineAligned(boolean) 若设置为false,将阻止该布局管理器与它的子元素的基线对齐 android:baselineAlignedChildIndex

Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout实例详解

本文实例分析了Android编程之绝对布局AbsoluteLayout和相对布局RelativeLayout.分享给大家供大家参考,具体如下: 一.绝对布局AbsoluteLayout 绝对定位AbsoluteLayout,又可以叫做坐标布局,可以直接指定子元素的绝对位置,这种布局简单直接,直观性强,但是由于手机屏幕尺寸差别比较大,使用绝对定位的适应性会比较差. 下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:la