用9path图片做背景 button和textview的文字不显示
2017-11-01 11:09
726 查看
(转载)http://blog.csdn.net/liao277218962/article/details/46876117
问题在于以下函数:
public void setBackgroundResource (int resid) Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove
the background.
也就是说,其实setBackgroundResource接着调用的应该是:setBackgroundDrawable函数,让我们看看setBackgroundDrawable的描述:
public void setBackgroundDrawable (Drawable d) Since: API Level 1 Set the background to a given Drawable, or remove the background.If the background has padding, this View's padding is set to the background's
padding. However, when a background is removed, this View's padding isn't touched. If setting the padding is desired, please use setPadding(int, int, int, int).Parametersd The Drawable to use as the background, or null to remove the background
也就是说,因为9-patch有自己的padding,所以convertView自己的padding被覆盖了!那可咋办呢?
其实知道这个了就很简单了,只要在代码setBackgroundResource之后加上(Remember:一定是这句之后!)一句:
view.setPadding(0, 0, 0, 0);
就可以解决问题了~
//------------------------------------------------------------------------------------------------------------
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="48px"
android:background="@drawable/icon_nav_message"
android:gravity="center"
android:minWidth="48px"
android:textSize="36px"
android:textColor="@color/white"/>
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("19");
tv.setBackgroundResource(R.drawable.icon_nav_message);
tv.setPadding(10, 0, 10, 0);
问题在于以下函数:
public void setBackgroundResource (int resid) Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove
the background.
也就是说,其实setBackgroundResource接着调用的应该是:setBackgroundDrawable函数,让我们看看setBackgroundDrawable的描述:
public void setBackgroundDrawable (Drawable d) Since: API Level 1 Set the background to a given Drawable, or remove the background.If the background has padding, this View's padding is set to the background's
padding. However, when a background is removed, this View's padding isn't touched. If setting the padding is desired, please use setPadding(int, int, int, int).Parametersd The Drawable to use as the background, or null to remove the background
也就是说,因为9-patch有自己的padding,所以convertView自己的padding被覆盖了!那可咋办呢?
其实知道这个了就很简单了,只要在代码setBackgroundResource之后加上(Remember:一定是这句之后!)一句:
view.setPadding(0, 0, 0, 0);
就可以解决问题了~
//------------------------------------------------------------------------------------------------------------
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="48px"
android:background="@drawable/icon_nav_message"
android:gravity="center"
android:minWidth="48px"
android:textSize="36px"
android:textColor="@color/white"/>
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText("19");
tv.setBackgroundResource(R.drawable.icon_nav_message);
tv.setPadding(10, 0, 10, 0);
相关文章推荐
- 用9path图片做背景 button和textview的文字不显示
- TextView设置背景图片为.9.png文字不显示
- TextView中显示文字和图片
- android中Textview 和图片同时显示时,文字省略号显示,图片自动靠到右边
- Android常用UI编程_TextView显示图片和文字(包含超链接)
- android--设置TextView部分文字的颜色和背景(高亮显示)
- android textview混合显示图片和文字
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- Android DrawableTextView图片文字居中显示实例
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- 如何使带背景图片的Button按钮中的文字居中偏上显示
- 安卓TextView显示图片与文字作为底部菜单
- android TextView如何通过setCompoundDrawables调整文字和图片的位置(文字显示在图片内)?
- textview中点击效果实现,比如点击textview中实现图片和文字的颜色变化(类似于button)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- 如何使带背景图片的Button按钮中的文字居中偏上显示
- android--设置TextView部分文字的颜色和背景(高亮显示)
- android圆形图片,圆形背景文字的CircleTextImageView开源组件
- android launcher主菜单显示图片和文字之BubbleTextView
- Android自定义TextView实现文字图片居中显示