
所以我将textOff和textOn分别设置为’male’和’female’,但根据开关位置,只显示男性或女性中的一个.
我怎样才能展示男性和女性?
所以,在ascii-art中
我有
[Male / ]or [ / Female ]
但我想要
[**Male** / Female][Male / **Female**]解决方法 嗯,你可以,但这有点麻烦.
这是我为此做到的:
我使用自定义drawable来跟踪开关. (轨道是拇指向左和向右滑动的容器.)
mMessengerSwitch.setTrackDrawable(new SwitchTrackTextDrawable(this,"left","RIGHT"));
这是SwitchTrackTextDrawable的实现,它将背景中的文本准确地写在正确的位置(好吧,我只测试了Nexus 5上的API 23):
/** * Drawable that generates the two pIEces of text in the track of the switch,one of each * sIDe of the positions of the thumb. */public class SwitchTrackTextDrawable extends Drawable { private final Context mContext; private final String mleftText; private final String mRightText; private final Paint mTextPaint; public SwitchTrackTextDrawable(@NonNull Context context,@StringRes int leftTextID,@StringRes int rightTextID) { mContext = context; // left text mleftText = context.getString(leftTextID); mTextPaint = createTextPaint(); // Right text mRightText = context.getString(rightTextID); } private Paint createTextPaint() { Paint textPaint = new Paint(); //noinspection deprecation textPaint.setcolor(mContext.getResources().getcolor(androID.R.color.white)); textPaint.setAntiAlias(true); textPaint.setStyle(Paint.Style.FILL); textPaint.setTextAlign(Paint.Align.CENTER); // Set textSize,typeface,etc,as you wish return textPaint; } @OverrIDe public voID draw(Canvas canvas) { final Rect textBounds = new Rect(); mTextPaint.getTextBounds(mRightText,mRightText.length(),textBounds); // The baseline for the text: centered,including the height of the text itself final int heightBaseline = canvas.getClipBounds().height() / 2 + textBounds.height() / 2; // This is one quarter of the full wIDth,to measure the centers of the texts final int wIDthQuarter = canvas.getClipBounds().wIDth() / 4; canvas.drawText(mleftText,mleftText.length(),wIDthQuarter,heightBaseline,mTextPaint); canvas.drawText(mRightText,wIDthQuarter * 3,mTextPaint); } @OverrIDe public voID setAlpha(int Alpha) { } @OverrIDe public voID setcolorFilter(colorFilter cf) { } @OverrIDe public int getopacity() { return PixelFormat.TRANSLUCENT; }} 总结 以上是内存溢出为你收集整理的android – 我可以在交换机中同时显示textOn和textOff吗?全部内容,希望文章能够帮你解决android – 我可以在交换机中同时显示textOn和textOff吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)