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程序设计有所帮助。

相关文章

  • Android Beam 文件传输失败分析与解决方法

    Android Beam 文件传输失败分析与解决方法

    最近在修改Android7.0原生平台的一些bug,其中有关Android Beam传输文件的一些问题还是挺多的。下面小编给大家带来了android beam 文件传输失败的解决方法,一起看看吧
    2017-09-09
  • Android SurfaceView拍照录像实现方法

    Android SurfaceView拍照录像实现方法

    这篇文章主要为大家详细介绍了Android SurfaceView拍照录像实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • android实现短按电源键关机的实现代码

    android实现短按电源键关机的实现代码

    这篇文章主要介绍了android实现短按电源键关机的实现代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Linux命令行下编译Android NDK的示例代码

    Linux命令行下编译Android NDK的示例代码

    今天小编就为大家分享一篇关于Linux命令行下编译Android NDK的示例代码,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Android实现打地鼠小游戏

    Android实现打地鼠小游戏

    这篇文章主要为大家详细介绍了Android实现打地鼠小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • Kotlin数据存储方式全面总结讲解

    Kotlin数据存储方式全面总结讲解

    在开发过程中,数据存取是较为频繁的,今天我们来了解下android几种常见的数据存取方式,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-12-12
  • Android编程实现加载等待ProgressDialog的方法

    Android编程实现加载等待ProgressDialog的方法

    这篇文章主要介绍了Android编程实现加载等待ProgressDialog的方法,实例分析了Android中加载等待类ProgressDialog的具体使用方法,需要的朋友可以参考下
    2015-12-12
  • Android Intent 用法全面总结及实例代码

    Android Intent 用法全面总结及实例代码

    这篇文章主要介绍了Android Intent 用法全面总结的相关资料,并附实例代码,需要的朋友可以参考下
    2016-09-09
  • Android编程实现分页加载ListView功能示例

    Android编程实现分页加载ListView功能示例

    这篇文章主要介绍了Android编程实现分页加载ListView功能,结合实例形式分析了listview分页加载的原理、实现技巧与相关注意事项,需要的朋友可以参考下
    2017-02-02
  • android使用DataBinding来设置空状态

    android使用DataBinding来设置空状态

    本篇文章主要介绍了android使用DataBinding来设置空状态,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-03-03

最新评论