Android布局控件之常用linearlayout布局

下面是“Android布局控件之常用LinearLayout布局”的完整攻略。

常用LinearLayout布局

LinearLayout布局简介

LinearLayout布局是Android中最基本、最常用的布局之一,其主要作用是将子控件按照线性方向依次排列。LinearLayout分为水平(horizontal)和垂直(vertical)两种方向,水平方向的LinearLayout中子控件从左到右排列,垂直方向的LinearLayout中子控件从上到下排列。LinearLayout的重要属性如下:

  • orientation:方向属性,可设为horizontal或vertical。
  • layout_weight:宽度或高度比重,当布局中的子控件没有设置具体的宽度或高度时,通过layout_weight属性可以实现布局中子控件的宽度或高度按照一定比例分配。
  • gravity:子控件对齐方式,可设为left、top、center等。

LinearLayout布局示例

下面通过两个网页布局的示例来进一步讲解LinearLayout布局。

示例一:水平布局

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="#F5F5F5"
    android:padding="12dp">

    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/avatar"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="10dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="John Smith"
            android:textSize="16sp"
            android:textColor="#333"
            android:maxLines="1"
            android:ellipsize="end"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Software Engineer"
            android:textSize="14sp"
            android:textColor="#999"
            android:maxLines="1"
            android:ellipsize="end"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ABC Company"
            android:textSize="14sp"
            android:textColor="#999"
            android:maxLines="1"
            android:ellipsize="end"/>
    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Follow"
        android:textColor="@color/colorPrimary"
        android:background="@drawable/btn_follow"/>

</LinearLayout>

以上代码实现的效果是一个水平排列的个人信息卡片,包括头像、姓名、职位和公司信息,以及一个“Follow”按钮。其中,头像和姓名信息在左侧,按钮在右侧,中间则显示职位和公司信息。关于代码中使用的一些属性,比如:background、padding、textSize、maxLines等也是比较常用的属性,不再进行讲解。

示例二:垂直布局

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:scaleType="centerCrop"
        android:src="@drawable/pic1"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="12dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Article Title"
            android:textSize="18sp"
            android:textColor="#333"
            android:maxLines="2"
            android:ellipsize="end"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Article Summary"
            android:textSize="14sp"
            android:textColor="#999"
            android:layout_marginTop="10dp"/>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#EEE"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="12dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Author:"
            android:textColor="#999"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="John Smith"
            android:textColor="#333"
            android:layout_marginLeft="6dp"/>

        <View
            android:layout_width="1dp"
            android:layout_height="12dp"
            android:background="#CCC"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Date:"
            android:textColor="#999"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2020/01/01"
            android:textColor="#333"
            android:layout_marginLeft="6dp"/>
    </LinearLayout>

</LinearLayout>

以上代码实现的效果是一个垂直布局的文章列表项,包括文章的标题、摘要、作者、日期等信息。其中,图片、标题和摘要排列在上方,而作者和日期信息则在下方水平排列。在此布局中,我们还使用了View来实现虚线分割线的效果。在TextView中,我们通过使用maxLines和ellipsize属性来设置文字的最大行数和省略方式。

总结

通过以上两个布局示例,我们可以看到LinearLayout在控制子控件排列方向、对齐方式等方面非常灵活,也比较容易上手。在实际开发中,我们可以根据UI设计稿和具体的布局需求选择合适的LinearLayout布局方向和属性设置。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android布局控件之常用linearlayout布局 - Python技术站

(0)
上一篇 2023年6月27日
下一篇 2023年6月27日

相关文章

  • layer插件学习——icon样式

    当然,我可以为您提供有关“layer插件学习——icon样式”的完整攻略,以下是详细说明: layer插件学习——icon样式 layer是一款基于jQuery的Web弹层组,可以用于实现各种弹层效果,包括提示框询问框、加载、页面层等。在layer中,可以使用icon样式为弹层添加图标,提高用户体验。以下是使用layer插件添加icon样式的方法: 方法一:…

    other 2023年5月7日
    00
  • Java 驼峰命名法详解(必看篇)

    Java 驼峰命名法详解(必看篇) 在Java编程中,驼峰命名法是一种常用的命名规范。它的命名风格简洁清晰,易于阅读和理解。本攻略将详细讲解驼峰命名法的使用方法和示例。 什么是驼峰命名法? 驼峰命名法是一种命名规范,其中单词之间没有下划线或其他分隔符,而是将每个单词的首字母大写,其余字母小写。这种命名方式使得变量、方法和类名更具可读性和一致性。 驼峰命名法的…

    other 2023年10月13日
    00
  • 如何查找ip地址 查找本地IP/网络IP/对方IP地址图文教程

    如何查找IP地址 IP地址是用于在网络中标识设备的唯一地址。在本教程中,我们将详细介绍如何查找本地IP地址、网络IP地址和对方IP地址。 查找本地IP地址 本地IP地址是指你的设备在局域网中的地址。以下是查找本地IP地址的步骤: 打开你的操作系统的命令提示符或终端窗口。 在命令提示符或终端窗口中输入以下命令:ipconfig(对于Windows系统)或ifc…

    other 2023年7月29日
    00
  • html5 video标签屏蔽右键视频另存为的js代码

    要实现html5 video标签屏蔽右键视频另存为,我们可以使用Javascript代码来解决。具体的实现过程如下: 1. 创建一个video标签 首先,我们需要在HTML中创建一个video标签,并指定要加载的视频文件路径。 <video id="myVideo" controls preload="metadata&q…

    other 2023年6月27日
    00
  • C语言单链表贪吃蛇小游戏

    C语言单链表贪吃蛇小游戏是一款基于C语言单链表数据结构开发的小游戏,玩家通过键盘控制贪吃蛇的移动,吃食物增加分数,碰到墙壁或者自身则游戏结束。下面是游戏攻略的详细说明。 游戏界面 贪吃蛇游戏界面一般由以下元素组成: 游戏主界面:包括游戏区域、当前分数和历史最高分等信息。 贪吃蛇:由若干个节点组成的一条蛇,初始为三节。 食物:随机生成的食物,被贪吃蛇吃到后会得…

    other 2023年6月27日
    00
  • Android开发之activity的生命周期详解

    Android开发之activity的生命周期详解 在Android开发过程中,Activity是一个非常重要的组件,掌握Activity的生命周期,能够更好的开发高质量的Android应用程序。本文将深入介绍Activity的生命周期,包括常见的生命周期回调方法和示例。 Activity的生命周期 Activity的生命周期是指Activity从被创建到被…

    other 2023年6月27日
    00
  • C语言函数超详细讲解上篇

    我们来详细讲解一下“C语言函数超详细讲解上篇”的完整攻略。 一、函数的定义 1.1 定义函数的语法结构 函数的定义包括函数头和函数体两部分。函数头的基本语法结构为: 返回类型 函数名(形参) 其中,返回类型指的是函数执行完毕后返回的结果类型,函数名是程序员自己定义的,用于在程序中调用函数;形参是函数体内部用到的变量,可以为空。 函数体需要用{}将其包裹,函数…

    other 2023年6月27日
    00
  • java多线程Thread-per-Message模式详解

    Java多线程Thread-per-Message模式详解 概述 Thread-per-Message是一种Java多线程模式,它是一种将任务和工作线程按需求一一对应的线程模型。Thread-per-Message模式的目的是去除传统多线程中必须使用锁和手动同步的麻烦。在这种模式下,当事件被触发时,一个新的线程被创建,并处理相关的任务。这个模式简化了开发者的…

    other 2023年6月27日
    00
合作推广
合作推广
分享本页
返回顶部