SwitchCompat 未在设备上显示

SwitchCompat not showing on device

我正在尝试使用 SwitchCompat,它显示在 Android Studio 的设计视图中,但在我的 Galaxy S4 (运行 5.0.1) 上,它没有出现。这是相关代码:

activity.xml

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginRight="20dp"
            >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/monitoring_text"
                android:id="@+id/textView8"
                android:layout_centerHorizontal="true" />

            <android.support.v7.widget.SwitchCompat
                android:id="@+id/monitoring_switch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="false"
                app:showText="false"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textView8"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

build.gradle

dependencies {
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.google.android.gms:play-services-appindexing:8.1.0'
}

这就是我最终让它工作的方式:

1 - 下载 Android 支持存储库

2 - 添加 xmlns:app="http://schemas.android.com/apk/res-auto" 到您的 activity XML

3 - 在您的活动中使用 AppCompat 版本的小部件,例如。导入 android.support.v7.widget.SwitchCompat;

4 - 确保将 styles.xml 和 AndroidManifest.xml 中的主题设置为 AppCompat 主题之一。

5 - 如果出现错误,请尝试使缓存无效并重新启动,以便重置 R 资源。