电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

詳解Android 硬布局item的高級寫法

瀏覽:60日期:2022-09-21 11:03:02

本文主要介紹了Android 硬布局item的高級寫法,分享給大家,具體如下:

效果:

詳解Android 硬布局item的高級寫法

這種布局應該是非常常見了,且寫的比較多。今天簡單探討一下效果圖中上下兩種布局的寫法。

比較

上下效果一致 行數(shù) 層級 上部分 121 3 下部分 55 2 下部分繼續(xù)精簡 28 2

可以看出,對比還是很明顯的,精簡到最后只有最開始的四分之一。

上部分

先看常規(guī)item寫法,橫向的LinearLayout嵌套三個子View,分別是

左邊的ImageView, 中間的TextView, 和右邊的ImageView。

然后每個橫向的LinearLayout之間添加一個高度1dp的View來作為橫線。

<LinearLayout 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'> <LinearLayout android: 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='http://www.hdgsjgj.cn/bcjs/@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='http://www.hdgsjgj.cn/bcjs/@mipmap/ic_arrow_right' /> </LinearLayout> <View android:layout_width='match_parent' android:layout_height='1dp' android:layout_marginStart='@dimen/dp_50' android:background='@color/color_F6F6F6' /> <LinearLayout android: 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='http://www.hdgsjgj.cn/bcjs/@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='http://www.hdgsjgj.cn/bcjs/@mipmap/ic_arrow_right' /> </LinearLayout> <View android:layout_width='match_parent' android:layout_height='1dp' android:layout_marginStart='@dimen/dp_50' android:background='@color/color_F6F6F6' /> <LinearLayout android: 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='http://www.hdgsjgj.cn/bcjs/@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='http://www.hdgsjgj.cn/bcjs/@mipmap/ic_arrow_right' /> </LinearLayout> </LinearLayout>

可以看到嵌套雖然不深,但是已經(jīng)拉的很長,不易閱讀修改。且 哪怕是一層的嵌套優(yōu)化,也是優(yōu)化,積少成多。

下部分

利用TextView的drawableStart和drawableEnd屬性,來做簡化,可以直接去掉左右兩邊的ImageView。至于分割線,利用LinearLayout的divider和showDividers屬性,寫個shape,來做簡化,去掉item之間做橫線的View。

<LinearLayout 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'> <TextView android: 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' /> <TextView android: 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' /> <TextView android: 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'> <item android:left='@dimen/dp_50' > <shape android:shape='rectangle'> <solid android:color='@color/color_F6F6F6' /> <size android: /> </shape> </item></layer-list>

可以看到,層級減少了,行數(shù)也減少了,看起來清爽多了。

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>

再看簡化后的代碼

<LinearLayout 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'> <TextView android: android:text='刪除個人信息' app:drawableStartCompat='@mipmap/ic_agreement' /> <TextView android: android:text='注銷賬戶' app:drawableStartCompat='@mipmap/ic_agreement' /> <TextView android: android:text='關于' app:drawableStartCompat='@mipmap/ic_agreement' /> </LinearLayout>

更加精簡了,只有簡化前的一半,共同屬性封裝,只需要關注業(yè)務參數(shù)。

核心屬性

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可去掉,小細節(jié) foreground,添加這個屬性會有水波紋的點擊效果,省了寫selector

到此這篇關于詳解Android 硬布局item的高級寫法的文章就介紹到這了,更多相關Android 硬布局item內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Android
相關文章:
主站蜘蛛池模板: 钣金加工厂家-钣金加工-佛山钣金厂-月汇好| VI设计-LOGO设计公司-品牌设计公司-包装设计公司-导视设计-杭州易象设计 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 尊享蟹太太美味,大闸蟹礼卡|礼券|礼盒在线预订-蟹太太官网 | 琉璃瓦-琉璃瓦厂家-安徽盛阳新型建材科技有限公司 | 杜甫仪器官网|实验室平行反应器|升降水浴锅|台式低温循环泵 | 东莞ERP软件_广州云ERP_中山ERP_台湾工厂erp系统-广东顺景软件科技有限公司 | 新型游乐设备,360大摆锤游乐设备「诚信厂家」-山东方鑫游乐设备 新能源汽车电池软连接,铜铝复合膜柔性连接,电力母排-容发智能科技(无锡)有限公司 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 武汉高低温试验机-现货恒温恒湿试验箱-高低温湿热交变箱价格-湖北高天试验设备 | led冷热冲击试验箱_LED高低温冲击试验箱_老化试验箱-爱佩百科 | 刑事律师_深圳著名刑事辩护律师_王平聚【清华博士|刑法教授】 | 塑料检查井_双扣聚氯乙烯增强管_双壁波纹管-河南中盈塑料制品有限公司 | 北京律师咨询_知名专业北京律师事务所_免费法律咨询 | 一氧化氮泄露报警器,二甲苯浓度超标报警器-郑州汇瑞埔电子技术有限公司 | 济南冷库安装-山东冷库设计|建造|冷库维修-山东齐雪制冷设备有限公司 | 灌木树苗-绿化苗木-常绿乔木-价格/批发/基地 - 四川成都途美园林 | 广州展览设计公司_展台设计搭建_展位设计装修公司-众派展览装饰 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 振动筛,震动筛,圆形振动筛,振动筛价格,振动筛厂家-新乡巨宝机电 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 手持气象站_便携式气象站_农业气象站_负氧离子监测站-山东万象环境 | 过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司 | 上海小程序开发-上海小程序制作公司-上海网站建设-公众号开发运营-软件外包公司-咏熠科技 | 电抗器-能曼电气-电抗器专业制造商 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | 考试试题_试卷及答案_诗词单词成语 - 优易学 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 全屋整木定制-橱柜,家具定制-四川峨眉山龙马木业有限公司 | 517瓜水果特产网|一个专注特产好物的网站 | 真石漆,山东真石漆,真石漆厂家,真石漆价格-山东新佳涂料有限公司 | 英国公司注册-新加坡公司注册-香港公司开户-离岸公司账户-杭州商标注册-杭州优创企业 | 信阳网站建设专家-信阳时代网联-【信阳网站建设百度推广优质服务提供商】信阳网站建设|信阳网络公司|信阳网络营销推广 | nalgene洗瓶,nalgene量筒,nalgene窄口瓶,nalgene放水口大瓶,浙江省nalgene代理-杭州雷琪实验器材有限公司 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 扬尘在线监测系统_工地噪声扬尘检测仪_扬尘监测系统_贝塔射线扬尘监测设备「风途物联网科技」 | 浙江筋膜枪-按摩仪厂家-制造商-肩颈按摩仪哪家好-温州市合喜电子科技有限公司 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 房屋质量检测-厂房抗震鉴定-玻璃幕墙检测-房屋安全鉴定机构 |