发布网友 发布时间:2024-09-28 18:25
共1个回答
热心网友 时间:2024-10-20 05:33
一般来说,我们在实际开发场景中都会碰到这种情况
一般来说我们会如下进行布局
但是要知道,ViewGroup也就是我们根布局,会对子View进行测量,每多一个View就会导致多测量一个View,而且我们设置点击也不好进行操作.当然如果你说再包一层那我确实没话说
所以我们需要一个更简洁更方便的方法来解决这种让人抓狂的问题
恰巧TextView就为我们提供了一个方式.
我们可以通过DrawableTop,DrawableBottom.DrawableLeft,DrawableRight为Textview的Drawable设置位置.但是新的问题出现了,Drawable的大小我们无法在Xml里进行控制,在不同的手机上他展示的效果很可能会不一致,甚至导致你的布局出现错乱.
当然你可以通过以下方式设置
如果你是独立开发,无所谓,如果是协同开发,你这么写会死人的。
这个时候就需要我们对TextView进行自定义了.
/***可自定义Drawable的TextView*strokeWidth可设置字体粗细中粗建议0.3f默认不加粗*/publicclassDrawableTextViewextendsandroidx.appcompat.widget.AppCompatTextView{privatefinalintDRAWABLE_LEFT=0;privatefinalintDRAWABLE_TOP=1;privatefinalintDRAWABLE_RIGHT=2;privatefinalintDRAWABLE_BOTTOM=3;privatefinalintleftDrawableWidth;privatefinalintleftDrawableHeight;privatefinalintrightDrawableWidth;privatefinalintrightDrawableHeight;privatefinalinttopDrawableWidth;privatefinalinttopDrawableHeight;privatefinalintbottomDrawableWidth;privatefinalintbottomDrawableHeight;privatefinalfloatstrokeWidth;privateintleftWidth,rightWidth;//左右图片宽度privateDrawableListener.DrawableRightListenerdrawableRightListener;privateDrawableListener.DrawableLeftListenerdrawableLeftListener;privateDrawableListener.DrawableTopListenerdrawableTopListener;privateDrawableListener.DrawableBottomListenerdrawableBottomListener;privateContextcontext;publicDrawableTextView(Contextcontext){this(context,null);init(context);}publicDrawableTextView(Contextcontext,AttributeSetattrs){this(context,attrs,0);init(context);}publicDrawableTextView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);TypedArraytypedArray=context.obtainStyledAttributes(attrs,R.styleable.DrawableTextView,defStyleAttr,0);leftDrawableHeight=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_leftDrawableHeight,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));leftDrawableWidth=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_leftDrawableWidth,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));rightDrawableHeight=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_rightDrawableHeight,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));rightDrawableWidth=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_rightDrawableWidth,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));topDrawableHeight=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_topDrawableHeight,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));topDrawableWidth=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_topDrawableWidth,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));bottomDrawableHeight=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_bottomDrawableHeight,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));bottomDrawableWidth=typedArray.getDimensionPixelSize(R.styleable.DrawableTextView_bottomDrawableWidth,(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,-1,getResources().getDisplayMetrics()));strokeWidth=typedArray.getFloat(R.styleable.DrawableTextView_StrokeWidth,0);typedArray.recycle();init(context);}privatevoidinit(Contextcontext){//自定义加粗程度建议0.3fif(strokeWidth!=0){TextPaintpaint=getPaint();paint.setStrokeWidth(strokeWidth);paint.setStyle(Paint.Style.FILL_AND_STROKE);}drawable();}privatevoiddrawable(){Drawable[]drawables=getCompoundDrawables();for(inti=0;i<drawables.length;i++){setDrawableSize(drawables[i],i);}//放置图片setCompoundDrawables(drawables[DRAWABLE_LEFT],drawables[DRAWABLE_TOP],drawables[DRAWABLE_RIGHT],drawables[DRAWABLE_BOTTOM]);}//设置drawableRight图片的点击监听publicvoidsetDrawableRightListener(DrawableListener.DrawableRightListenerdrawableRightListener){this.drawableRightListener=drawableRightListener;}publicvoidsetDrawableLeftListener(DrawableListener.DrawableLeftListenerdrawableLeftListener){this.drawableLeftListener=drawableLeftListener;}publicvoidsetDrawableTopListener(DrawableListener.DrawableTopListenerdrawableTopListener){this.drawableTopListener=drawableTopListener;}publicvoidsetDrawableBottomListener(DrawableListener.DrawableBottomListenerdrawableBottomListener){this.drawableBottomListener=drawableBottomListener;}@OverridepublicbooleanonTouchEvent(MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_UP:if(drawableRightListener!=null){DrawabledrawableRight=getCompoundDrawables()[DRAWABLE_RIGHT];if(drawableRight!=null&&event.getRawX()>=(getRight()-drawableRight.getBounds().width())&&event.getRawX()<getRight()){drawableRightListener.drawableRightListener(this);returntrue;}}if(drawableLeftListener!=null){DrawabledrawableLeft=getCompoundDrawables()[DRAWABLE_LEFT];if(drawableLeft!=null&&event.getRawX()<=(getLeft()+drawableLeft.getBounds().width())&&event.getRawX()>getLeft()){drawableLeftListener.drawableLeftListener(this);returntrue;}}if(drawableTopListener!=null){DrawabledrawableTop=getCompoundDrawables()[DRAWABLE_TOP];if(drawableTop!=null&&event.getRawY()<=(getTop()+drawableTop.getBounds().height())&&event.getRawY()>getTop()){drawableTopListener.drawableTopListener(this);returntrue;}}if(drawableBottomListener!=null){DrawabledrawableBottom=getCompoundDrawables()[DRAWABLE_BOTTOM];if(drawableBottom!=null&&event.getRawY()>=(getBottom()-drawableBottom.getBounds().height())&&event.getRawY()<getBottom()){drawableBottomListener.drawableBottomListener(this);returntrue;}}break;}returnsuper.onTouchEvent(event);}@OverrideprotectedvoidonDraw(Canvascanvas){super.onDraw(canvas);}//设置图片的高度和宽度privatevoidsetDrawableSize(Drawabledrawable,intindex){if(drawable==null){return;}//左上右下intwidth=0,height=0;switch(index){caseDRAWABLE_LEFT:width=leftDrawableWidth;height=leftDrawableHeight;break;caseDRAWABLE_TOP:width=topDrawableWidth;height=topDrawableWidth;break;caseDRAWABLE_RIGHT:width=rightDrawableWidth;height=rightDrawableWidth;break;caseDRAWABLE_BOTTOM:width=bottomDrawableWidth;height=bottomDrawableHeight;break;}//如果没有设置图片的高度和宽度具使用默认的图片高度和宽度if(width<0){width=drawable.getIntrinsicWidth();}if(height<0){height=drawable.getIntrinsicHeight();}if(index==0){leftWidth=width;}elseif(index==2){rightWidth=width;}drawable.setBounds(0,0,width,height);}}必要的注释,我已经写在代码里了.里面其实我做了一些自己的常用的封装..
对于Drawable的点击监听,因为有时候实际的业务场景很复杂,阴间的产品需求可能会让你只能对Drawable点击触发事件
设置字体的粗细程度,Textview我们虽然可以设置是否粗细,但是官方提供的API设置的太粗了,你仔细看蓝湖上的字体粗细,有很大差别.
以下是使用方式
上面可以看到我的StrokeWidth是3,这个我给予的是float,你按照自己的感觉设置即可.
监听要设置你Drawable位置的监听,如果你的xml是DrawableLeft,在Activity里你设置的DrawableTop点击事件他是不会生效的.
为了方便大家复制下面是attrs里的配置
<!--drawableTextView--><declare-styleablename="DrawableTextView"><attrname="leftDrawableWidth"format="dimension"/><attrname="leftDrawableHeight"format="dimension"/><attrname="rightDrawableWidth"format="dimension"/><attrname="rightDrawableHeight"format="dimension"/><attrname="topDrawableWidth"format="dimension"/><attrname="topDrawableHeight"format="dimension"/><attrname="bottomDrawableWidth"format="dimension"/><attrname="bottomDrawableHeight"format="dimension"/><attrname="StrokeWidth"format="float"/></declare-styleable>其实上面的封装没有什么高深的东西,都是一些基础,不过一些涉及的东西比较乱而已,大家不用太注意细节,只要能解决项目中的实际问题即可。