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

 更新时间:2016年01月06日 15:11:23   作者:jzp12  
这篇文章主要介绍了Android编程之线性布局LinearLayout用法,结合实例形式简单分析了Android线性布局的使用技巧,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Json 生成与解析详解及实例代码

    Json 生成与解析详解及实例代码

    这篇文章主要介绍了Json 生成与解析详解及实例代码的相关资料,这里附简单实例帮助大家学习理解,需要的朋友可以参考下
    2016-11-11
  • Android解析服务器端发来的xml数据示例

    Android解析服务器端发来的xml数据示例

    Android跟服务器交互数据,有时数据量大时,就需要以xml形式的交互数据,下面与大家分享下使用XmlPullParser来解析xml数据,感兴趣的朋友可以参考下哈
    2013-06-06
  • Android 中使用 dlib+opencv 实现动态人脸检测功能

    Android 中使用 dlib+opencv 实现动态人脸检测功能

    完成 Android 相机预览功能以后,在此基础上我使用 dlib 与 opencv 库做了一个关于人脸检测的 demo。接下来通过本文给大家介绍Android 中使用 dlib+opencv 实现动态人脸检测功能 ,需要的朋友可以参考下
    2018-11-11
  • Android属性动画实现炫酷的登录界面

    Android属性动画实现炫酷的登录界面

    这篇文章主要为大家详细介绍了Android属性动画实现炫酷的登录界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • 详细分析Android-Zygote的启动过程

    详细分析Android-Zygote的启动过程

    在Android系统中,所有的应用程序进程以及系统服务进程SystemServer都是由Zygote进程孕育(fork)出来的,这也许就是为什么要把它称为Zygote(受精卵)的原因吧。由于Zygote进程在Android系统中有着如此重要的地位,本文将详细分析它的启动过程
    2021-06-06
  • Android6.0仿微信权限设置

    Android6.0仿微信权限设置

    这篇文章主要为大家详细介绍了Android6.0仿微信权限设置的相关内容,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Android 使用Fragment模仿微信界面的实例代码

    Android 使用Fragment模仿微信界面的实例代码

    自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其目的是为了解决不同屏幕分辩率的动态和灵活UI设计。下面通过本文给大家分享Android 使用Fragment模仿微信界面的实例代码,需要的的朋友参考下吧
    2017-07-07
  • Android 监听应用前/后台切换实例代码

    Android 监听应用前/后台切换实例代码

    本篇文章主要介绍了Android 监听应用前/后台切换实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Android实现自定义dialog的代码

    Android实现自定义dialog的代码

    这篇文章主要介绍了Android实现自定义dialog的实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-11-11
  • Android Studio使用ButterKnife和Zelezny的方法

    Android Studio使用ButterKnife和Zelezny的方法

    这篇文章主要为大家详细介绍了Android Studio使用ButterKnife和Zelezny的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-04-04

最新评论