Adroid 硬布局item的高级写法

2024-04-03 13:38
文章标签 布局 高级 写法 item adroid

本文主要是介绍Adroid 硬布局item的高级写法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

效果:

在这里插入图片描述

这种布局应该是非常常见了,且写的比较多。

今天简单探讨一下效果图中上下两种布局的写法。

比较

上下效果一致行数层级
上部分1213
下部分552
下部分继续精简282

可以看出,对比还是很明显的,精简到最后只有最开始的四分之一。

上部分

先看常规item写法,横向的LinearLayout嵌套三个子View,分别是

  • 左边的ImageView,
  • 中间的TextView,
  • 和右边的ImageView。

然后每个横向的LinearLayout之间添加一个高度1dp的View来作为横线。

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_15"android:layout_marginTop="@dimen/dp_20"android:layout_marginEnd="@dimen/dp_15"android:layout_marginBottom="@dimen/dp_20"android:background="@drawable/shape_bg_white"android:orientation="vertical"><LinearLayoutandroid:id="@+id/ll1"android:layout_width="match_parent"android:layout_height="wrap_content"android:foreground="?android:attr/selectableItemBackground"android:gravity="center_vertical"android:orientation="horizontal"android:padding="@dimen/dp_20"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name"android:src="@mipmap/ic_agreement" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_20"android:layout_weight="1"android:includeFontPadding="false"android:text="删除个人信息"android:textColor="@color/color_505258"android:textSize="@dimen/sp_14" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name"android:src="@mipmap/ic_arrow_right" /></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:layout_marginStart="@dimen/dp_50"android:background="@color/color_F6F6F6" /><LinearLayoutandroid:id="@+id/ll2"android:layout_width="match_parent"android:layout_height="wrap_content"android:foreground="?android:attr/selectableItemBackground"android:gravity="center_vertical"android:orientation="horizontal"android:padding="@dimen/dp_20"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name"android:src="@mipmap/ic_agreement" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_20"android:layout_weight="1"android:includeFontPadding="false"android:text="注销账户"android:textColor="@color/color_505258"android:textSize="@dimen/sp_14" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name"android:src="@mipmap/ic_arrow_right" /></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:layout_marginStart="@dimen/dp_50"android:background="@color/color_F6F6F6" /><LinearLayoutandroid:id="@+id/ll3"android:layout_width="match_parent"android:layout_height="wrap_content"android:foreground="?android:attr/selectableItemBackground"android:gravity="center_vertical"android:orientation="horizontal"android:padding="@dimen/dp_20"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name"android:src="@mipmap/ic_agreement" /><TextViewandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginStart="@dimen/dp_20"android:layout_weight="1"android:includeFontPadding="false"android:text="关于"android:textColor="@color/color_505258"android:textSize="@dimen/sp_14" /><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:contentDescription="@string/app_name"android:src="@mipmap/ic_arrow_right" /></LinearLayout></LinearLayout>
复制代码

可以看到嵌套虽然不深,但是已经拉的很长,不易阅读修改。

且 哪怕是一层的嵌套优化,也是优化,积少成多。

下部分

利用TextView的drawableStart和drawableEnd属性,来做简化,可以直接去掉左右两边的ImageView。

至于分割线,利用LinearLayout的divider和showDividers属性,写个shape,来做简化,去掉item之间做横线的View。

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginHorizontal="@dimen/dp_15"android:layout_marginVertical="@dimen/dp_20" android:background="@drawable/shape_bg_white"android:divider="@drawable/shape_divider_my"android:orientation="vertical"android:showDividers="middle"><TextViewandroid:id="@+id/tv_delete_user"android:layout_width="match_parent"android:layout_height="wrap_content"android:drawablePadding="@dimen/dp_16"android:foreground="?android:attr/selectableItemBackground"android:gravity="center_vertical"android:includeFontPadding="false"android:padding="@dimen/dp_20"android:text="删除个人信息"android:textColor="@color/color_505258"android:textSize="@dimen/sp_14"app:drawableEndCompat="@mipmap/ic_arrow_right"app:drawableStartCompat="@mipmap/ic_agreement" /><TextViewandroid:id="@+id/tv_logout_user"android:layout_width="match_parent"android:layout_height="wrap_content"android:drawablePadding="@dimen/dp_16"android:foreground="?android:attr/selectableItemBackground"android:gravity="center_vertical"android:includeFontPadding="false"android:padding="@dimen/dp_20"android:text="注销账户"android:textColor="@color/color_505258"android:textSize="@dimen/sp_14"app:drawableEndCompat="@mipmap/ic_arrow_right"app:drawableStartCompat="@mipmap/ic_agreement" /><TextViewandroid:id="@+id/tv_about"android:layout_width="match_parent"android:layout_height="wrap_content"android:drawablePadding="@dimen/dp_16"android:foreground="?android:attr/selectableItemBackground"android:gravity="center_vertical"android:includeFontPadding="false"android:padding="@dimen/dp_20"android:text="关于"android:textColor="@color/color_505258"android:textSize="@dimen/sp_14"app:drawableEndCompat="@mipmap/ic_arrow_right"app:drawableStartCompat="@mipmap/ic_agreement" /></LinearLayout>
复制代码

shape:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:left="@dimen/dp_50" ><shape android:shape="rectangle"><solid android:color="@color/color_F6F6F6" /><size android:height="1dp" /></shape></item>
</layer-list>
复制代码

可以看到,层级减少了,行数也减少了,看起来清爽多了。

style简化

尽管如此,我们还是有可以简化的空间。

TextView有一些共同属性,可以抽取做一个style。

    <style name="MyTextView"><item name="android:layout_width">match_parent</item><item name="android:layout_height">wrap_content</item><item name="android:drawablePadding">@dimen/dp_16</item><item name="android:foreground">?android:attr/selectableItemBackground</item><item name="android:gravity">center_vertical</item><item name="android:includeFontPadding">false</item><item name="android:padding">@dimen/dp_20</item><item name="android:textColor">@color/color_505258</item><item name="android:textSize">@dimen/sp_14</item><item name="drawableEndCompat">@mipmap/ic_arrow_right</item></style>
复制代码

再看简化后的代码

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginHorizontal="@dimen/dp_15"android:layout_marginVertical="@dimen/dp_20" android:background="@drawable/shape_bg_white"android:divider="@drawable/shape_divider_my"android:orientation="vertical"android:showDividers="middle"><TextViewandroid:id="@+id/tv_delete_user"style="@style/MyTextView"android:text="删除个人信息"app:drawableStartCompat="@mipmap/ic_agreement" /><TextViewandroid:id="@+id/tv_logout_user"style="@style/MyTextView"android:text="注销账户"app:drawableStartCompat="@mipmap/ic_agreement" /><TextViewandroid:id="@+id/tv_about"style="@style/MyTextView"android:text="关于"app:drawableStartCompat="@mipmap/ic_agreement" /></LinearLayout>
复制代码

更加精简了,只有简化前的一半,共同属性封装,只需要关注业务参数。

核心属性

LinearLayout

  • divider,分割线
  • showDividers,分割线的显示方式
  • layout_marginVertical,代替原来的layout_marginTop、layout_marginBottom
  • layout_marginHorizontal,代替原来的layout_marginStart、layout_marginEnd

题外话,LinearLayout的android:animateLayoutChanges="true",可以在其子view添加移除的时候添加简单的动画。

TextView

  • drawableEndCompat,即原来的drawableEnd,设置右边的drawable,其他方向同理
  • drawablePadding,drawable与文字之前的内边距
  • includeFontPadding,TextView默认top是有6dp的padding的,false可去掉,小细节
  • foreground,添加这个属性会有水波纹的点击效果,省了写selector

 

ok,到此结束,无聊的知识又增加了。

这篇关于Adroid 硬布局item的高级写法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/873061

相关文章

Java中的for循环高级用法

《Java中的for循环高级用法》本文系统解析Java中传统、增强型for循环、StreamAPI及并行流的实现原理与性能差异,并通过大量代码示例展示实际开发中的最佳实践,感兴趣的朋友一起看看吧... 目录前言一、基础篇:传统for循环1.1 标准语法结构1.2 典型应用场景二、进阶篇:增强型for循环2.

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

mybatis的mapper对应的xml写法及配置详解

《mybatis的mapper对应的xml写法及配置详解》这篇文章给大家介绍mybatis的mapper对应的xml写法及配置详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录前置mapper 对应 XML 基础配置mapper 对应 xml 复杂配置Mapper 中的相

mysql中的group by高级用法详解

《mysql中的groupby高级用法详解》MySQL中的GROUPBY是数据聚合分析的核心功能,主要用于将结果集按指定列分组,并结合聚合函数进行统计计算,本文给大家介绍mysql中的groupby... 目录一、基本语法与核心功能二、基础用法示例1. 单列分组统计2. 多列组合分组3. 与WHERE结合使

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2

PyTorch高级特性与性能优化方式

《PyTorch高级特性与性能优化方式》:本文主要介绍PyTorch高级特性与性能优化方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、自动化机制1.自动微分机制2.动态计算图二、性能优化1.内存管理2.GPU加速3.多GPU训练三、分布式训练1.分布式数据

Spring Boot集成SLF4j从基础到高级实践(最新推荐)

《SpringBoot集成SLF4j从基础到高级实践(最新推荐)》SLF4j(SimpleLoggingFacadeforJava)是一个日志门面(Facade),不是具体的日志实现,这篇文章主要介... 目录一、日志框架概述与SLF4j简介1.1 为什么需要日志框架1.2 主流日志框架对比1.3 SLF4

Spring Boot集成Logback终极指南之从基础到高级配置实战指南

《SpringBoot集成Logback终极指南之从基础到高级配置实战指南》Logback是一个可靠、通用且快速的Java日志框架,作为Log4j的继承者,由Log4j创始人设计,:本文主要介绍... 目录一、Logback简介与Spring Boot集成基础1.1 Logback是什么?1.2 Sprin

MySQL复合查询从基础到多表关联与高级技巧全解析

《MySQL复合查询从基础到多表关联与高级技巧全解析》本文主要讲解了在MySQL中的复合查询,下面是关于本文章所需要数据的建表语句,感兴趣的朋友跟随小编一起看看吧... 目录前言:1.基本查询回顾:1.1.查询工资高于500或岗位为MANAGER的雇员,同时还要满足他们的姓名首字母为大写的J1.2.按照部门