【问题标题】:Android Switch widget inside AlertDialog, not showing thumb image (on/off instead)AlertDialog 中的 Android Switch 小部件,不显示拇指图像(改为打开/关闭)
【发布时间】:2017-10-26 23:38:37
【问题描述】:

我有一个相当简单的对话框,里面有一个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>

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

注意小写字母中的“开”和“关”。这里没有自定义图形或类,开箱即用。

当我在Activity 中这样做时,我不明白。相反,我得到了正常的拇指图像,这正是我想要的:

即使我扩大 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 版本对于此类没有区别。

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

【问题讨论】:

    标签: android android-alertdialog uiswitch


    【解决方案1】:

    试试这个:

        <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 版本中的外观和感觉都相同的唯一可靠方法。

    如果有更好的建议欢迎留言。

    【讨论】:

    • 好吧,我认为这不是我所问问题的答案。也就是说,为什么 Switch 在 AlertDialog 中和在 Activity 中时看起来不同?
    • 如果没有其他方法,我可能会将其用作解决方法。
    • @Emmanuel 您使用的是support libraryAlertDialog 版本吗?我认为这是由于library 中的drawable 损坏所致。
    【解决方案2】:

    使用这个

        <Switch
            android:id="@+id/switchCalls"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:textOff=""
            android:textOn=""
             />
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      相关资源
      最近更新 更多