多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android中layout_align****和setPadding方法整合

Android中layout_align****和setPadding方法整合

來源:程序員人生   發布時間:2016-03-07 08:45:08 閱讀次數:2928次

轉載請注明出處:http://blog.csdn.net/u012975705/article/details/49282835

前言

最近在項目中遇到1個問題,要求顯示下面的效果。
這里寫圖片描述

這里寫圖片描述

如圖所示,“所屬農莊”必須緊挨在“商品名字”后面,但當商品名字太長時必須使得所屬農莊顯示完全,并且商品名字中顯示不全的部份使用省略號,開始1直沒弄出來,后面想到用layout_align****,才成功實現其效果。
其他不說,先來來看看layout_align**** 的用法。

layout_align**** 介紹

layout_align**** 是RelativeLayout布局中子控件所具有的1個用來幫助肯定顯示位置屬性。align翻譯過來為對齊,所以layout_align**** 即為與某某對其,先看例子:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/shop_item_name" style="@style/TextNormal" android:layout_width="wrap_content" android:layout_height="match_parent" android:singleLine="true" android:ellipsize="end" android:text="商品名字商品名字" /> <TextView android:layout_alignRight="@id/shop_item_name" android:layout_alignBaseline="@id/shop_item_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="所屬農莊" android:textColor="@color/red" android:textSize="@dimen/typeface_micro_12" /> </RelativeLayout>

顯示效果:
這里寫圖片描述

其中

layout_alignRight="@id/shop_item_name"

表示與id=shop_item_name的控件又對齊,即“所屬農莊”的右側緣與“商品名字”的右側緣重合。

layout_align** ="@id/shop_item_name"

顧名思義,是該兩個控件的** 邊重合。
固然RelativeLayout中還有些比較特別的控件,
比如

android:layout_alignBaseline="@id/shop_item_name"

表示“所屬農莊”控件中的文字與“商品名字”控件中的文字的下邊對齊;
比如

android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true"

分別表示與該控件的父控件的左、上、右、下邊沿對齊

效果實現具體方式

上面代碼中的實現效果
這里寫圖片描述

明顯不符合要求,想要實現符合要求的布局必須在控件“商品名字”中添加代碼:

android:paddingRight="控件'所屬農場'的長度"

其中padding** 即該控件中的內容距該控件** 邊沿的距離
完全代碼以下:

<RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/shop_item_name" style="@style/TextNormal" android:paddingRight="控件'所屬農場'的長度" android:layout_width="wrap_content" android:layout_height="match_parent" android:singleLine="true" android:ellipsize="end" android:text="商品名字" /> <TextView android:layout_alignRight="@id/shop_item_name" android:layout_alignBaseline="@id/shop_item_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="所屬農莊" android:textColor="@color/red" android:textSize="@dimen/typeface_micro_12" /> </RelativeLayout>

實現效果:
這里寫圖片描述

下面介紹其中觸及到的怎樣動態獲得控件的長度和動態設置控件的padding值
1、動態獲得控件長度
在獲得控件高寬的時候不能直接在代碼中使用

int width = holder.mTvName.getWidth();

來獲得,由于只有在measure方法被調用以后才能獲得到控件的真實長度,所以必須通過利用ViewTreeObserver 監聽來獲得控件高寬。使用方式以下:

ViewTreeObserver vto = holder.mTvName.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { holder.mTvName.getViewTreeObserver().removeGlobalOnLayoutListener(this); int width = holder.mTvName.getWidth(); } });

2、在代碼中設置控件的padding

holder.shopName.setPadding(left, top, right, bottom);

設置控件中內容距離控件左、上、右、下邊沿的距離,單位為px

完全代碼:

ViewTreeObserver vto = holder.mTvName.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { holder.mTvName.getViewTreeObserver().removeGlobalOnLayoutListener(this); int width = holder.mTvName.getWidth(); holder.shopName.setPadding(0, 0, width + 5, 0); } });

版權聲明:本文為博主原創文章,未經博主允許不得轉載。博客首頁:http://blog.csdn.net/u012975705

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲精品国产网红在线一区 | 校园春色亚洲 | 最近中文字幕高清mv免费 | 欧美13一14周岁a在线播放 | 亚洲欧美另类精品久久久 | 亚洲欧洲日产国码在线观看 | 亚洲精品乱码中文字幕无线 | 国产在线原创剧情麻豆 | 日本无卡无吗中文免费 | 最近免费中文字幕大全高清片 | 黄色网址网站在线观看 | 国产精美视频 | 欧美激情一区二区三区在线播放 | 伊人久久成人成综合网222 | 午夜 性色 福利视频 | 丁香九月婷婷 | 日本精品一区二区三区在线观看 | 波多野结衣一区二区三区四区 | 成人无遮挡毛片免费看 | 美女免费观看一区二区三区 | 一级毛片a女人刺激视频免费 | 国产一区二区色淫影院 | 黄色aa视频| 99日韩精品 | 五月婷婷在线免费观看 | 免费在线成人网 | 亚洲免费网站观看视频 | 亚洲欧美片 | 久久亚洲国产最新网站 | 一区二区三区四区国产精品 | 九操网| 欧美中文小说在线观看 | 激情爱爱网站 | 羞羞网站在线看 | 91国内| 99久久这里只精品麻豆 | 中文在线第一页 | 日本www在线观看 | 国产高清在线精品一区二区三区 | 天堂mv亚洲mv在线播放9蜜 | 精品国产第一国产综合精品 |