android on off控件,关于android:我可以在开关中同时显示textOn和textOff吗?

我有一个Switch,它将在Male和Female之间进行选择。

因此,我分别将textOff和textOn设置为" male"和" female",但是根据开关位置,仅显示male或female中的一个。

如何显示男性和女性?

因此,在ascii-art中

我有

[Male /        ]

or

[     / Female ]

但是我想要

[**Male** / Female]

[Male / **Female**]

stackoverflow.com/questions/19356349/

好的,您可以,但是有点麻烦。

这是我为获得此目的所做的:

26b98975ccca29a2e0b44b595b4ded17.png

我将自定义可绘制对象用于开关的轨迹。 (轨道是拇指在其中左右滑动的容器。)

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, 0, 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, 0, mLeftText.length(),

widthQuarter, heightBaseline,

mTextPaint);

canvas.drawText(mRightText, 0, mRightText.length(),

widthQuarter * 3, heightBaseline,

mTextPaint);

}

@Override

public void setAlpha(int alpha) {

}

@Override

public void setColorFilter(ColorFilter cf) {

}

@Override

public int getOpacity() {

return PixelFormat.TRANSLUCENT;

}

}

我发现获得所要外观的最简单方法是一对相邻的邻接按钮。 Onclick,我交换背景色。

您可以制作两个同时具有的图像(用于切换的图像和用于切换的图像),然后创建选择器。

只需将textOn和textOff更改为空字符串即可。

谢谢你的建议。 我尽量避免使用图片,因此一直在寻找一种标记解决方案。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值