在愉快的一天实现后,这是一个完整的、可行的解决方案。
使用以下代码来设置开关轨道的可绘制图形。轨道是指拇指左右滑动的容器。
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;
}
}
这是我最终的样式,添加了更多样式,例如我给包装它的更新:与此同时,该库已停止维护,您可能需要尝试他们推荐的库。
FrameLayout
加了一个白色边框(我需要让它看起来像这样,您不需要使用边框):
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1dp"
android:background="@drawable/white_border">
<belka.us.androidtoggleswitch.widgets.ToggleSwitch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:activeBgColor="@color/white"
custom:activeTextColor="@color/black"
custom:inactiveBgColor="@color/black"
custom:inactiveTextColor="@color/white"
custom:textToggleLeft="left"
custom:textToggleRight="right"/>
</FrameLayout>
而@drawable/white_border
看起来像这样:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="@android:color/transparent" />
<stroke android:width="2dip"
android:color="@color/white" />
<corners
android:radius="3dp"/>
android:textOn
和android:textOff
,并且我得到了两个文本,但问题是,它只显示一个文本,另一个文本同时消失。我想要显示两个文本,无论它是开启还是关闭。这样用户就可以看到其他可用选项。 - Mayur Raval<color name="switch_selected_text_color">#FFFFFF</color>
<color name="switch_regular_text_color">#A8A8A8</color>
设置_切换颜色选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/switch_selected_text_color" android:state_checked="true" />
<item android:color="@color/switch_regular_text_color" />
</selector>
styles.xml
<style name="SwitchTextAppearance" parent="@android:style/TextAppearance.Holo.Small">
<item name="android:textColor">@color/settings_switch_color_selector</item>
</style>
new_switch.xml - 用于自定义视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/track_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/settings_track"
android:weightSum="1">
<TextView
android:id="@+id/left_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textColor="@color/switch_regular_text_color"
android:layout_weight="0.5"
android:gravity="center"
android:text="OFF" />
<TextView
android:id="@+id/right_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:textColor="@color/switch_regular_text_color"
android:layout_weight="0.5"
android:gravity="center"
android:text="ON" />
</LinearLayout>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="@drawable/thumb_selector"
android:switchTextAppearance="@style/SwitchTextAppearance"
android:textOn="ON"
android:textOff="OFF"
android:checked="true"
android:showText="true"
android:track="@android:color/transparent"/>
</RelativeLayout>
这是自定义视图 - 它只是用于填充自定义视图布局
public class DoubleSidedSwitch extends RelativeLayout {
private TextView _leftTextView;
private TextView _rightTextView;
private Switch _switch;
public DoubleSidedSwitch(Context context) {
super(context);
init(context);
}
public DoubleSidedSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.new_switch, this, true);
initViews(view);
initListeners();
}
private void initListeners() {
}
private void initViews(View view) {
}
}
这个开关是由两个标准按钮和一个LinearLayout组成的。有一堆xml文件需要导入,但它在所有版本上都能完美地工作,并且非常容易使用。请查看以下Github页面
SekizbitSwitch mySwitch = new SekizbitSwitch(findViewById(R.id.sekizbit_switch));
mySwitch.setOnChangeListener(new SekizbitSwitch.OnSelectedChangeListener() {
@Override
public void OnSelectedChange(SekizbitSwitch sender) {
if(sender.getCheckedIndex() ==0 )
{
System.out.println("Left Button Selected");
}
else if(sender.getCheckedIndex() ==1 )
{
System.out.println("Right Button Selected");
}
}
});