色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

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

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

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

效果:

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

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

比較

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

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

上部分

先看常規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.lshqa.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.lshqa.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.lshqa.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.lshqa.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.lshqa.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.lshqa.cn/bcjs/@mipmap/ic_arrow_right' /> </LinearLayout> </LinearLayout>

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

下部分

利用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>

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

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>

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

核心屬性

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
相關文章:
主站蜘蛛池模板: 久久久久欧美精品网站 | 精品久久久久国产 | 91精品成人免费国产 | 国产毛片一区 | 99视频在线播放 | 久久国产亚洲观看 | 国内精品久久久久久久久久影视 | 成人男女网18免费91 | 精品国产一区二区三区免费 | 日韩精品一区二区三区免费观看 | 欧美手机手机在线视频一区 | 国产天堂在线一区二区三区 | 免费欧洲毛片a级视频 | 兔子先生节目在线观看免费 | 99欧美精品| 特级淫片欧美高清视频蜜桃 | 欧美成人精品久久精品 | 狠狠综合久久久久综合 | 毛片在线不卡 | 亚洲成年男人的天堂网 | 久久精品国内偷自一区 | 免费看一级做a爰片久久 | 国产成人一区二区视频在线观看 | 亚洲精品日本高清中文字幕 | 国产成人免费网站在线观看 | 欧美特级特黄a大片免费 | 国内自拍网站 | 成人看的午夜免费毛片 | 男人都懂的网址在线看片 | 免费一级a毛片在线播放 | 欧美性久久久久 | 精品一区二区三区四区在线 | 手机福利在线 | 成人亚洲综合 | 中国一级毛片在线观看 | 国产在线乱子伦一区二区 | 亚洲天堂视频在线观看免费 | 欧美真人视频一级毛片 | 国产香蕉98碰碰久久人人 | 日本精品久久久久久久久免费 | 亚洲欧美成人影院 |