日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

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

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

轉載請注明出處: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

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 人人干天天干 | 欧美视频网址 | 久久精品久久久精品美女 | 精品美女| 久久国产精品一区 | 自拍在线观看 | 欧美日韩免费在线观看 | 日韩精品不卡 | 亚洲精品一二三 | 男女爱爱免费视频 | 亚洲精品乱码久久久久久写真 | 欧美精品一区二区在线观看 | 国产剧情在线 | 亚洲精品在线视频网站 | 女人18毛片九区毛片在线 | 久热这里只有精品6 | av成人在线观看 | 麻豆视频在线观看免费网站黄 | 国产欧美日韩在线观看 | 在线看片日韩 | 日韩精品一区二区在线 | 免费成人在线看 | a视频在线观看免费 | 国产精品成人一区二区三区 | 久久91精品国产一区二区三区 | av在线一区二区 | 亚洲网站在线看 | 国产一区二区三区四区五区入口 | 九九热在线视频观看 | 国产一区在线播放 | 国产不卡一二三区 | 国产大片在线免费观看 | 国产精品永久免费 | 精品在线播放 | 黄色日b | 欧美亚洲国产日韩 | 久久久久国产精品视频 | 韩日电影在线观看 | 欧美日韩黑人 | 在线国产视频 | 在线一区二区三区 |