LinearLayou(线性布局布局)
一些重要的属性:
一orientation(朝向) 该属性值有两种一种是垂直朝向(verticle),还有一个是水平朝向(horizontal)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@drawable/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- view1 goes on top -->
<TextView
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/linear_layout_1_top"/>
<!-- view2 goes in the middle -->
<TextView
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/linear_layout_1_middle"/>
<!-- view3 goes on the bottom -->
<TextView
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/linear_layout_1_bottom"/>
</LinearLayout>
结果如下:
二layout_weight(权重):
看下面一个例子: 该布局填充整个屏幕,其中有三个字控件,分别占据头部,底部,中间
在上一篇博客中我们通过相对布局也可以实现(把高设置成0,height=0)
更多关于该属性的细节可以浏览http://hi.baidu.com/mendynew/item/39cd374192770bab60d7b915
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/blue"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- view1 goes on top -->
<TextView
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/linear_layout_3_top"/>
<!-- view2 goes in the middle -->
<TextView
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/linear_layout_3_middle"/>
<!-- view3 goes on the bottom -->
<TextView
android:background="@drawable/box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/linear_layout_3_bottom"/>
</LinearLayout>
运行结果:
下面一个例子,所有子空间的都是相同的宽度.也是通过该属性来实现的.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:background="@drawable/red"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:background="@drawable/green"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:background="@drawable/blue"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView
android:background="@drawable/yellow"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
下面看一个简单表单的例子,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/blue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/linear_layout_5_instructions"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/linear_layout_5_cancel"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:text="@string/linear_layout_5_ok" />
</LinearLayout>
</LinearLayout>
在上一篇博客中通过相对布局也能布局出这样的,但是从效率上说,相对布局要好很多,效率要高.从这个例子上看线性布局的层级要深.
weight属性还可以实现如下布局:
运行结果:
通过相对布局也是可以实现这样的布局,把button设置为android:layout_alignParentBottom ="true"
三gravity(重心)
下面来看一个例子:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="30dip" >
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue"
android:orientation="vertical"
android:padding="30dip" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/box"
android:text="@string/linear_layout_8_c" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/box"
android:text="@string/linear_layout_8_b" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/box"
android:text="@string/linear_layout_8_c" />
</LinearLayout>
</FrameLayout>
Java代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, VERTICAL_ID, 0, R.string.linear_layout_8_vertical);
menu.add(0, HORIZONTAL_ID, 0, R.string.linear_layout_8_horizontal);
menu.add(0, TOP_ID, 0, R.string.linear_layout_8_top);
menu.add(0, MIDDLE_ID, 0, R.string.linear_layout_8_middle);
menu.add(0, BOTTOM_ID, 0, R.string.linear_layout_8_bottom);
menu.add(0, LEFT_ID, 0, R.string.linear_layout_8_left);
menu.add(0, CENTER_ID, 0, R.string.linear_layout_8_center);
menu.add(0, RIGHT_ID, 0, R.string.linear_layout_8_right);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case VERTICAL_ID:
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
return true;
case HORIZONTAL_ID:
mLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
return true;
case TOP_ID:
mLinearLayout.setVerticalGravity(Gravity.TOP);
return true;
case MIDDLE_ID:
mLinearLayout.setVerticalGravity(Gravity.CENTER_VERTICAL);
return true;
case BOTTOM_ID:
mLinearLayout.setVerticalGravity(Gravity.BOTTOM);
return true;
case LEFT_ID:
mLinearLayout.setHorizontalGravity(Gravity.LEFT);
return true;
case CENTER_ID:
mLinearLayout.setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
return true;
case RIGHT_ID:
mLinearLayout.setHorizontalGravity(Gravity.RIGHT);
return true;
}
return super.onOptionsItemSelected(item);
}
以上设置的gravity是通过Java代码设置的,也可以通过xml配置
需要注意的是layout_gravity和gravity的区别,前者是该控件相对于父控件的重心(gravity),后者该控件的子空间的重心(gravity)
欢迎转载,转载请注明出处:http://blog.csdn.net/johnny901114/article/details/7866864
分享到:
相关推荐
关于linearlayout布局案例
Android中线性布局LinearLayout的特点.pdf 学习资料 复习资料 教学资源
-- 声明一个 LinearLayout 布局,并设置其属性 --> 9 10 android:text="@string/add" 11 android:id="@+id/Button01" 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content"> 14 ...
android布局和控件 LinearLayout:线性布局,从左到右 第一种方法实现界面布局:手动写布局 第二种方法实现界面布局:布局文件(layout中的activty_main.xml) 其实就是标签的嵌套,注意单标签、双标签 拨打电话的...
Android 布局控件之LinearLayout详细介绍,需要的朋友可以参考一下
在做android UI布局时,用了LinearLayout嵌套,发现效果并不如我预料一般 查了下资料,说是要设置layout_weight属性 资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱。 于是静下心来,自己写xml测试,发现...
Android 开发 (实验五) 实验题目:... 二、实验内容 组 件 布 局 有 : LinearLayout 、 TableLayout 、 FrameLayout 、 RelativeLayout 三、实验步骤 1、用表格布局完成登录界面 <TableLayout xmlns:android=...
首先说的是LinearLayout布局下的居中一般是这样的: (注意:android:layout_width=”fill_parent” android:layout_height=”fill_parent” 属性中,若水平居中,至少在宽度上占全屏;若垂直居中,则在高度上占全屏...
LinearLayout - 线形布局。 orientation - 容器内元素的排列方式。vertical: 子元素们垂直排列;horizontal: 子元素们水平排列 gravity - 内容的排列形式。常用的有 top, bottom, left, right, center 等,详见...
主要介绍了Android布局之LinearLayout自定义高亮背景的方法,实例分析了Android中LinearLayout布局参数设置技巧,需要的朋友可以参考下
LinearLayout是线性布局控件:要么横向排布,要么竖向排布,下面通过本篇文章给大家介绍Android布局之LinearLayout线性布局,涉及到android linearlayout 布局相关知识,对本文感兴趣的朋友一起学习吧
www.mars-droid.com/Android开发视频教程 LinearLayout TableLayout代码 源码 mars老师讲课 android 视频源码 Layout_04(在此特别感谢mars的无私奉献,此代码为跟随视频边学边做的)
Android开发学习23】界面布局之线性布局LinearLayout代码
代码如下:<...LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_p
android studio 初学者 Linearlayout 水平和垂直的布局文件
利用LinearLayout布局实现 计算器界面 充分利用weight属性
LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:id=”@+id/LinearLayout” android:layout_width=”fill_parent” android...
布局:TwoPageLayout作为父布局,内部有且只能有两个子控件ScrollEndScrollView 详细介绍请看:自定义LinearLayout实现淘宝详情页 License Copyright 2016 LineChen...