在Android中,线性布局是程序中最常见的一种布局方式。

线性布局可以分为水平线性布局和垂直线性布局两种,通过android:orientation属性可以设置线性布局的方向。

android:gravity
该属性用于控制布局中控件的对齐方式。如果是没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式;若是有子控件的控件设置此属性,则表示其子控件的对齐方式。

android: layout_weight
通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1,那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸;对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度,再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度。

下面通过一个简单的例子来实现LinearLayout线性布局。

假如我们需要实现如下效果,该如何实现呢


下面,我们来仔细分析一下这个布局文件

这个图片被一个垂直线性布局分成了两部分,而且是均等的两部分,如图所示


图片上半部分是水平线性布局,其中左边和右边各占1/4,中间占1/2。

图片下半部分是垂直线性布局,其中上中两块分别占1/4,底部占1/2。

其中文字的位置也不相同。


分析完成后,可以开始布局了。

布局界面activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayoutandroid:orientation="horizontal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"><TextViewandroid:text="红色"android:gravity="center_horizontal|center_vertical"android:background="#aa0000"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"/><TextViewandroid:text="绿色"android:gravity="center_horizontal"android:background="#00aa00"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="2"/><TextViewandroid:text="蓝色"android:gravity="center|bottom"android:background="#0000aa"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"/></LinearLayout><LinearLayoutandroid:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_weight="1">    <TextViewandroid:text="第一行"android:gravity="left|top"        android:textSize="15sp"        android:background="#ff0000"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"/>    <TextView        android:text="第二行"        android:gravity="center"        android:textSize="15sp"        android:background="#00ff00"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"/>    <TextView        android:text="第三行"        android:gravity="right|bottom"        android:textSize="15sp"        android:background="#0000ff"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="2"/></LinearLayout></LinearLayout>

主程序MainActivity.java

package com.sl.linearlayoutdemo;import android.os.Bundle;import android.app.Activity;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

OK,这样就完成啦!


更多相关文章

  1. android:ellipsize="end" + android:maxLines="" 失败的原因与解
  2. Android高手进阶教程(四)之----Android(安卓)中自定义属性(attr.
  3. Android之替换APP字体——Typeface
  4. 认识一下Android(安卓)事件分发机制
  5. Android(安卓)实用工具Hierarchy Viewer实战
  6. android实现防美拍点赞效果
  7. 初学Android,使用自定义的XML资源(十八)
  8. Android高手进阶教程(四)之----Android(安卓)中自定义属性(attr.
  9. Android(安卓)UI 之TextView控件中可选择的属性列表,开发中经常用

随机推荐

  1. 程序员30岁前月薪达不到30K,该何去何从
  2. Python正则表达式的运用与常用的正则表达
  3. Webpack 4正式发布!从0配置到生产模式,你需
  4. js中基础数据结构数组去重问题
  5. 混合内容下的浏览器行为 [每日前端夜话(0
  6. 偷学Python第五天|循环结构,附练习案例
  7. 我在北京前端面试那些事!
  8. 聊聊 Array 中的一个小坑 [每日前端夜话(
  9. 手把手教你使用Pygame制作飞机大战小游戏
  10. 理解Redis单线程运行模式