【问题标题】:Can I display both textOn and textOff in a switch?我可以在开关中同时显示 textOn 和 textOff 吗?
【发布时间】:2013-12-30 03:47:32
【问题描述】:

我有一个可以在男性和女性之间进行选择的开关。

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

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

所以,在 ascii-art 中

我有

[Male /        ]
or 
[     / Female ]

但我想要

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

【问题讨论】:

标签: android android-layout


【解决方案1】:

你可以,但是有点麻烦。

为了得到这个,我做了以下工作:

我使用自定义可绘制对象作为开关的轨道。 (轨道是拇指左右滑动的容器。)

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

【讨论】:

    【解决方案2】:

    您可以制作两个同时具有的图像(一个用于打开和关闭的图像),然后创建一个selector

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

    【讨论】:

    • 感谢您的建议。我尽量避免使用图片,所以一直在寻找标记解决方案。
    【解决方案3】:

    我发现获得所需外观的最简单方法是使用一对相邻的按钮。单击时,我交换背景颜色。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多