AlertDialog中的Android Switch小部件,不显示拇指图像(代替开/关)

时间:2017-10-26 23:38:38

标签: android alertdialog uiswitch

我有一个内部有ArrayAdapter的简单对话框。适配器膨胀以下布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp">

    <TextView
        android:id="@+id/txt_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_toStartOf="@+id/calendar_selection_switch"
        android:ellipsize="end"
        android:text="test test"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <Switch
        android:id="@id/calendar_selection_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_gravity="end|center_vertical"
        android:layout_marginStart="10dp"
        android:minHeight="40dp"
        android:minWidth="80dp" />
</RelativeLayout>

当我显示此对话框时,这就是我得到的:

AlertDialog without the thumb image

注意&#34; on&#34;和&#34;关&#34;用小写字母。这里没有自定义图形或类,只是开箱即用。

当我在Activity中执行此操作时,我无法理解。相反,我得到了正常的拇指图像,这正是我想要的:

enter image description here

即使我增加了Switch的大小,它也没有任何区别。它只是使列表的行更大。

这是构建和显示对话框的代码:

AlertDialog.Builder builderSingle = new AlertDialog.Builder(this);
builderSingle.setTitle(R.string.cal_title);

builderSingle.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
    }
});

final CalendarListAdapter cla = new CalendarListAdapter(this, calendarList, null);
builderSingle.setAdapter(cla, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        cla.toggleSelect(which);
    }
});
builderSingle.setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (saveCalendars(cla)) {
            dialog.dismiss();
        }
    }
});
AlertDialog dialog = builderSingle.create();
dialog.show();

CalendarListAdapter是一个简单的ArrayAdapter

public class CalendarListAdapter extends ArrayAdapter<Calendar> {

虽然此类的Activity版本和AlertDialog版本之间没有区别。

我很困惑。我在这里做的并不是很复杂。请帮忙。

1 个答案:

答案 0 :(得分:0)

试试这个:

    <Switch
        android:id="@+id/calendar_selection_switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_gravity="end|center_vertical"
        android:layout_marginStart="10dp"
        android:minHeight="40dp"
        android:minWidth="80dp"
        android:textOn=""
        android:textOff=""
        android:switchMinWidth="48.0sp"
        android:thumb="@drawable/switch_selector"
        android:track="@drawable/switch_track"
        android:checked="false"/>

switch_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true">
        <shape
            android:shape="rectangle"
            android:visible="true"
            android:useLevel="false">
            <gradient
                android:startColor="@color/white"
                android:endColor="@color/white"
                android:angle="270"/>
            <corners
                android:radius="14.0sp"/>
            <size
                android:width="28.0sp"
                android:height="28.0sp" />
            <stroke
                android:width="4.0sp"
                android:color="#11000000"/>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape
            android:shape="rectangle"
            android:visible="true"
            android:dither="true"
            android:useLevel="false">
            <gradient
                android:startColor="@color/white"
                android:endColor="@color/white"
                android:angle="270"/>
            <corners
                android:radius="16.0sp"/>
            <size
                android:width="28.0sp"
                android:height="28.0sp" />
            <stroke
                android:width="2.0sp"
                android:color="#11000000"/>
        </shape>
    </item>
</selector>

switch_track.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:visible="true"
    android:useLevel="false">
    <gradient
        android:startColor="@color/white"
        android:endColor="@color/white"
        android:angle="270"/>
    <stroke
        android:width="2.0sp"
        android:color="#11000000"/>
    <corners
        android:radius="14.0sp"/>
    <size
        android:width="42.0sp"
        android:height="28.0sp" />
</shape>

然后在代码中你可以这样做:

        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    aSwitch.setTrackResource(R.drawable.switch_track_off);
                } else {
                    aSwitch.setTrackResource(R.drawable.switch_track);
                }
            }
        });

switch_track_off.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    android:visible="true"
    android:useLevel="false">
    <gradient
        android:startColor="#00A4FD"
        android:endColor="#00A4FD"
        android:angle="270"/>
    <stroke
        android:width="2.0sp"
        android:color="#00A4FC"/>
    <corners
        android:radius="14.0sp"/>
    <size
        android:width="42.0sp"
        android:height="28.0sp" />
</shape>

您可以根据需要更改颜色和其他样式

据我所知,这是使Switch在应用中的所有Android版本中外观和感觉相同的唯一可靠方式。

如果有更好的建议,请发表评论。