自定义TextView,去掉没有文字时的空白区域
来源:互联网 发布:中箭mod 知乎 编辑:程序博客网 时间:2024/05/21 04:18
1.需求
文字和icon的布局采用TextView的setCompoundDrawables()来实现。
2.遇到的问题
垂直布局,icon在上面,文字在下面,当没有文字的时候,下方文字区域会有一片空白。
3.解决办法
在onMeasure()中做手脚,当文字为空时,我们自己手动设计宽高。关键代码:
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (getText().length() == 0) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int w = iconWidth; int h = iconHeight; if (widthMode == MeasureSpec.EXACTLY) { w = widthSize; } if (heightMode == MeasureSpec.EXACTLY) { h = heightSize; } setMeasuredDimension(w, h); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}
4.完整代码
class IconTextView extends TextView { public static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3; int iconWidth; int iconHeight; int iconOrientation = TOP; public IconTextView(Context context) { super(context); setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); setGravity(Gravity.CENTER); setCompoundDrawablePadding(0); } public void setIcon(int resId) { if (resId > 0) { final Resources resources = getContext().getResources(); Drawable icon = resources.getDrawable(resId); int width = iconWidth == 0 ? icon.getIntrinsicWidth() : iconWidth; int height = iconHeight == 0 ? icon.getIntrinsicHeight() : iconHeight; icon.setBounds(0, 0, width, height); switch (iconOrientation) { case LEFT: setCompoundDrawables(icon, null, null, null); break; case TOP: setCompoundDrawables(null, icon, null, null); break; case RIGHT: setCompoundDrawables(null, null, icon, null); break; case BOTTOM: setCompoundDrawables(null, null, null, icon); break; } } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (getText().length() == 0) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int w = iconWidth; int h = iconHeight; if (widthMode == MeasureSpec.EXACTLY) { w = widthSize; } if (heightMode == MeasureSpec.EXACTLY) { h = heightSize; } setMeasuredDimension(w, h); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }}
2015年12月18日21:26:32
0 0
- 自定义TextView,去掉没有文字时的空白区域
- iOS去掉UITableView分组时上方空白区域的方法
- 消除TextView中的文本与TextView边界的空白区域
- Android GridView 去掉GridView左和上的空白区域
- Android GridView 去掉GridView左和上的空白区域
- Android TextView 文字空白解决方案
- 清除Android TextView 周围空白区域
- 自定义竖直旋转显示文字的TextView
- 自定义竖直旋转显示文字的TextView
- Android自定义TextView闪烁文字的效果
- 自定义竖直旋转显示文字的TextView
- 自定义textview旋转文字
- Android textview文字两端对齐,中间空白
- 自定义View--文字两端能够对齐的TextView,文字右端能够对齐的TextView
- 关于textview中表示文字段落时,特定区域显示高亮颜色和点击事件的方法
- ListView Item只能点击有文字的区域,空白处点击不了。
- 自定义Label文字绘制区域
- 自定义actionbar 并去掉左边空白
- 数组内存存储细节
- cocoapod
- 科普:手机5G WiFi 那些事(二)
- WiFi 信号强度和网速有关系吗?
- SQL 2008在还原时如何重建LDF文件
- 自定义TextView,去掉没有文字时的空白区域
- Thread与Service没有关系
- Qt之重启应用程序
- POJ2240 Floyd
- hdu2152 Fruit (背包dp)
- leetcode -- Kth Largest Element in an Array -- 很巧妙的方法,记住
- NSValue用法
- Revit2016二次开发
- caffe