spiner 具有android drawableeleft属性吗

【Android】自定义控件让TextView的drawableLeft与文本一起居中显示
TextView的drawableLeft、drawableRight和drawableTop是一个常用、好用的属性,可以在文本的上下左右放置一个图片,而不使用更加复杂布局就能达到,我也常常喜欢用RadioButton的这几个属性实现很多效果,但是苦于不支持让drawbleLeft与文本一起居中,设置gravity为center也无济于事,终于有空研究了一下,这里与大家一起分享。
欢迎转载,请注明出处!
博客园:http://www.cnblogs.com/
农民伯伯: http://www.cnblogs.com/over140/
一、效果图
二、实现代码
自定义控件
* drawableLeft与文本一起居中显示
* @author 农民伯伯
* @see http://www.cnblogs.com/over140/p/3464348.html
*/public class DrawableCenterTextView extends TextView {
public DrawableCenterTextView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
public DrawableCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
public DrawableCenterTextView(Context context) {
super(context);
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (drawables != null) {
Drawable drawableLeft = drawables[0];
if (drawableLeft != null) {
float textWidth = getPaint().measureText(getText().toString());
int drawablePadding = getCompoundDrawablePadding();
int drawableWidth = 0;
drawableWidth = drawableLeft.getIntrinsicWidth();
float bodyWidth = textWidth + drawableWidth + drawableP
canvas.translate((getWidth() - bodyWidth) / 2, 0);
super.onDraw(canvas);
和普通TextView用法一致,无需额外增加属性。
注意,drawableRight不管用!感谢网友提醒,后续有进展了再更新文章。
那些让你难受的技术问题一定要找时间想办法摆平TA!
转载:http://www.cnblogs.com/over140/p/3464348.html(梦中浮生)
(梦中浮生)
(梦中浮生)
第三方登录:android的drawableleft能设置shape吗_百度知道
android的drawableleft能设置shape吗
我有更好的答案
utton上的android:drawableLeft设置的图片就是居左,无法和文字一起居中,文字属性可以通过android:layout_gravity设置居中,想要android,android:两张图片合二为一,android:drawableLeft设置的图片直接和按钮背景图合到一张图片上:drawableLeft设置的图片居中:drawableLeft设置的图片大约位于按钮背景图三分之一处(黄金分割点),右侧空出文本区域,可以通过另外的方法来实现,建议
VpnVip资讯教程,软件使用平台。
操作系统/系统故障
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。drawableleft 代码里面怎么设置_百度知道
drawableleft 代码里面怎么设置
我有更好的答案
解决方案:Java codepublic void
setCompoundDrawables
(Drawable left, Drawable top, Drawable right, Drawable bottom);原问题来自于CSDN问答频道,更多解决方案见:htt p:/ /ask.csdn.n et/q uestions/903类似调用方法如下:1.在XML中使用Java codeandroid:drawableLeft=&@drawable/icon&2.代码中动态变化Java codeDrawable drawable= getResources().getDrawable(R.drawable.drawable);/// 这一步必须要做,否则不会显示.drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());myTextview.setCompoundDrawables(drawable,null,null,null);
采纳率:95%
来自团队:
为您推荐:
其他类似问题
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。新浪广告共享计划>
广告共享计划
动态设置android:drawableLeft|Right|Top|Bottom
Android中有时需动态设置控件四周的drawble图片,这个时候就需要调用
setCompoundDrawables(left, top, right,
bottom),四个参数类型都是drawable
Button继承TextView,所以可以采用相同的设置方法
方法一.XML方式
方法二.JAVA代码
Drawable img_on,
Resources res =
getResources();
res.getDrawable(R.drawable.btn_strip_mark_off);
//调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
img_off.setBounds(0, 0,
img_off.getMinimumWidth(),
img_off.getMinimumHeight());
btn.setCompoundDrawables(img_off, null, null,
null); //设置左图标
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 drawable hdpi 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信