Android代码设置TextView的顶部图及设置图与字体之间距离
2018-02-07 23:56
549 查看
终于建了一个自己个人小站:https://huangtianyu.gitee.io,以后优先更新小站博客,欢迎进站,O(∩_∩)O~~
现在很多的设计底部都是几个Tab标签,每个标签都是上边是图片,下边是文字。下面提供一种简单的方式来实现这个:
<TextView
android:id="@+id/tv_versionstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:drawableTop="@drawable/icon_home"
android:text="首页"
android:textColor="#363636"
android:textSize="20sp" />
然后在代码中设置图片和图片与文字间的间距。代码设置如下:
//设置TextView的顶部图标,其中setCompoundDrawablesWithIntrinsicBounds对应的是left,top,right,bottom图标
private void setHomeMenuTextDrawableTop(TextView textView, Drawable drawable) {
if (textView == null || drawable == null) {
return;
}
textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
textView.invalidate();
}
通过代码设置图片和文字之间的间距:
textView.setCompoundDrawablePadding(padding) ;//int padding 代码设置文字和图片间距
这里说个问题,就是有时候设置了图片和文字间距在实际使用时并没有看到效果,这是因为view的宽度过宽,导致view内文本和图片间距过大,那我们可以通过设置paddingLeft、paddingRight、paddingTop、paddingBottom来缩小这个间距.
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="@string/xian_txt"
android:drawableRight="@mipmap/ic_triangle_down"
android:background="@android:color/transparent"
android:drawablePadding="6dp"
android:gravity="center"
android:paddingRight="24dp"
android:paddingLeft="24dp"
/>
现在很多的设计底部都是几个Tab标签,每个标签都是上边是图片,下边是文字。下面提供一种简单的方式来实现这个:
<TextView
android:id="@+id/tv_versionstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:drawableTop="@drawable/icon_home"
android:text="首页"
android:textColor="#363636"
android:textSize="20sp" />
然后在代码中设置图片和图片与文字间的间距。代码设置如下:
//设置TextView的顶部图标,其中setCompoundDrawablesWithIntrinsicBounds对应的是left,top,right,bottom图标
private void setHomeMenuTextDrawableTop(TextView textView, Drawable drawable) {
if (textView == null || drawable == null) {
return;
}
textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
textView.invalidate();
}
通过代码设置图片和文字之间的间距:
textView.setCompoundDrawablePadding(padding) ;//int padding 代码设置文字和图片间距
这里说个问题,就是有时候设置了图片和文字间距在实际使用时并没有看到效果,这是因为view的宽度过宽,导致view内文本和图片间距过大,那我们可以通过设置paddingLeft、paddingRight、paddingTop、paddingBottom来缩小这个间距.
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="@string/xian_txt"
android:drawableRight="@mipmap/ic_triangle_down"
android:background="@android:color/transparent"
android:drawablePadding="6dp"
android:gravity="center"
android:paddingRight="24dp"
android:paddingLeft="24dp"
/>
相关文章推荐
- Android: TextView 及其子类通过代码和 XML 设置字体大小的存在差异的分析
- Android 代码设置TextView 字体颜色--ColorStateList
- Android 代码中设置TextView字体颜色
- Android 代码中动态设置字体大小-TextView.SetTextSize()
- android textview 代码设置字体大小包含适配
- android Java代码设置textview的字体资源颜色
- 【转】Android中设置TextView的颜色setTextColor--代码中设置字体颜色
- Android 代码中动态设置字体大小-TextView.SetTextSize()
- Android在代码中设置textview字体颜色
- Android TextView内容设置超链接、颜色、字体
- Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格
- android 的TextView空间动态设置字体颜色
- Android TextView设置字体风格
- ANDROID TEXTVIEW 设置字体颜色
- Android TextView当中设置超链接、颜色、字体
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- Android中TextView中的字体大小能设置吗?
- android TextView 结合SpannableString对部分内容设置颜色、字体、超链接、图片
- 【工作笔记】android TextView、EditText对部分内容设置颜色、字体、超链接、图片
- android中用Spannable在TextView中设置超链接、颜色、字体