gpt4 book ai didi

android - 我可以在一个开关中同时显示 textOn 和 textOff 吗?

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:26 26 4
gpt4 key购买 nike

我有一个 Switch,可以在男性和女性之间进行选择。

所以我将 textOff 和 textOn 分别设置为“男性”和“女性”,但根据开关位置,只显示男性或女性之一。

如何让它同时显示男性和女性?

所以,在 ascii-art 中

我有

[Male /        ]
or
[ / Female ]

但是我想要

[**Male** / Female]
[Male / **Female**]

最佳答案

可以,但是有点麻烦。

这是我为实现此目的所做的工作:

Custom switch example

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

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;
}
}

关于android - 我可以在一个开关中同时显示 textOn 和 textOff 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20531538/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com