android 最新写法,详解Android 硬布局item的高级写法
本文主要介绍了Android 硬布局item的高级写法,分享给大家,具体如下:
效果:
这种布局应该是非常常见了,且写的比较多。
今天简单探讨一下效果图中上下两种布局的写法。
比较
上下效果一致
行数
层级
上部分
121
3
下部分
55
2
下部分继续精简
28
2
可以看出,对比还是很明显的,精简到最后只有最开始的四分之一。
上部分
先看常规item写法,横向的LinearLayout嵌套三个子View,分别是
左边的ImageView,
中间的TextView,
和右边的ImageView。
然后每个横向的LinearLayout之间添加一个高度1dp的View来作为横线。
android: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">
android: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">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_agreement" />
android: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" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_arrow_right" />
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="@dimen/dp_50"
android:background="@color/color_F6F6F6" />
android: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">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_agreement" />
android: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" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_arrow_right" />
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginStart="@dimen/dp_50"
android:background="@color/color_F6F6F6" />
android: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">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_agreement" />
android: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" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_arrow_right" />
可以看到嵌套虽然不深,但是已经拉的很长,不易阅读修改。
且 哪怕是一层的嵌套优化,也是优化,积少成多。
下部分
利用TextView的drawableStart和drawableEnd属性,来做简化,可以直接去掉左右两边的ImageView。
至于分割线,利用LinearLayout的divider和showDividers属性,写个shape,来做简化,去掉item之间做横线的View。
android: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">
android: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" />
android: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" />
android: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" />
shape:
android:left="@dimen/dp_50" >
可以看到,层级减少了,行数也减少了,看起来清爽多了。
style简化
尽管如此,我们还是有可以简化的空间。
TextView有一些共同属性,可以抽取做一个style。
match_parent
wrap_content
@dimen/dp_16
?android:attr/selectableItemBackground
center_vertical
false
@dimen/dp_20
@color/color_505258
@dimen/sp_14
@mipmap/ic_arrow_right
再看简化后的代码
android: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">
android:id="@+id/tv_delete_user"
style="@style/MyTextView"
android:text="删除个人信息"
app:drawableStartCompat="@mipmap/ic_agreement" />
android:id="@+id/tv_logout_user"
style="@style/MyTextView"
android:text="注销账户"
app:drawableStartCompat="@mipmap/ic_agreement" />
android:id="@+id/tv_about"
style="@style/MyTextView"
android:text="关于"
app:drawableStartCompat="@mipmap/ic_agreement" />
更加精简了,只有简化前的一半,共同属性封装,只需要关注业务参数。
核心属性
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
到此这篇关于详解Android 硬布局item的高级写法的文章就介绍到这了,更多相关Android 硬布局item内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
android 最新写法,详解Android 硬布局item的高级写法相关推荐
- 视频教程-Android Studio 开发详解-Android
Android Studio 开发详解 1999年开始从事开发工作,具备十余年的开发.管理和培训经验. 在无线通信.Android.iOS.HTML5.游戏开发.JavaME.JavaEE.Linux ...
- android标签table,详解Android TableLayout表格布局
表格布局的标签是TableLayout,TableLayout继承了LinearLayout.所以它依然是一个线性布局. 前言: 1.TableLayout简介 2.TableLayout行列数的确定 ...
- 【Android】Realm详解(Android 数据库Sqlite的完美替代者)
介绍 Realm 是一个 MVCC (多版本并发控制)数据库,由Y Combinator公司在2014年7月发布一款支持运行在手机.平板和可穿戴设备上的嵌入式数据库,目标是取代SQLite. Real ...
- android 系统设置向导,详解Android 手机卫士设置向导页面
推荐阅读: 设置向导页面,通过SharedPreferences来判断是否已经设置过了,跳转到不同的页面 自定义样式 在res/values/styles.xml中 添加节点 在 在的文本里面,设置布 ...
- android 界面 edittext详解,Android EditText详解
前言 很常用的控件EditText(输入框): 和TextView非常类似,最大的区别是:EditText可以接受用户输入.和前面一样,我们不一个个讲属性, 只讲实际应用.那么开始本节内容! 1.设置 ...
- activity启动模式 android,专题:详解Android组件Activity的启动模式singleTask_51CTO.COM
在Android应用程序中,可以配置Activity以四种方式来启动,其中最令人迷惑的就是"singleTask"这种方式了,官方文档称以这种方式启动的Activity总是属于一个 ...
- convert android layout xml,详解Android之解析XML文件三种方式(DOM,PULL,SAX)
1.xml文件代码 ${fq.content} ${fq.time} 2.XML网页效果图 3.Android代码 1.布局文件 xmlns:tools="http://schemas.an ...
- android 命令使用详解,Android下pm 命令详解
作者:Sam (甄峰) sam_code@hotmail.com Sam在看相关PackageManager代码时,无意中发现Android 下提供一个pm命令,通常放在/system/bin/下.这 ...
- android检查新版本,详解Android Studio无法检测新版本问题解决
大家都知道Android Studio可以直接在"Menu - Check for Updates..."自动检测并更新版本,还可以在弹出的检查框中点击"Updates& ...
最新文章
- 代码审查就是在排大便——你懂的!
- mysql cluster 查看最大索引数_MySQL 数据库优化,看这篇就够了
- 为什么商家数字化离不开交易平台
- anconda设置镜像源_管理2000+Docker镜像,Kolla是如何做到的
- tornado基本使用一
- 明晰监管范围保护信息安全
- python 双冒号_python双冒号
- android apk安装包 华为提示安装包无效或与操作系统不兼容,魅族提示apk仅为测试版,要求下载正式版安装
- C语言基础and进阶——猜拳游戏(6个版本)
- 用计算机弹课间进行曲,课间进行曲(修改稿)
- kubectl查看node状态_【大强哥-k8s从入门到放弃03】查看K8S集群基本信息
- 2008年8月25号,星期一,晴。天将降大任于是人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为。
- 做项目经理的自我总结(一)
- java-从date中获取年份
- 【QImage类常用函数】
- .net6API使用AutoMapper和DTO
- html、css、js(javaWEB开发)
- iOS开发学无止境 - UIStackView如何让你的开发更简单
- As4741G安装WindowsXP后的驱动安装方法
- 决策树构建算法—ID3、C4.5、CART树
热门文章
- arcgis for android sdk下载地址,Arcgis Runtime sdk for android 授权
- php mysql 框架 php5.2_简单快速安装Apache+PHP+MySql服务环境(四)—— 将php版本升级到7.2...
- 简单粗暴地理解js原型链--js面向对象编程
- 用自己电脑做服务器,建个人网站
- window.open被浏览器拦截的解决方案
- 自定义的plot函数参数date坐标模型[x,y]的使用建议
- Gobblin编译支持CDH5.4.0
- matlab循环遍历数组_MatLab简易教程 #8 循环
- Win10+CUDA10.0.130+cudnn7.4.1+tensorflow1.13.1+anaconda3 5.2.0+GTX1060
- 广度优先搜索——岛屿数量(Leetcode 200)