我正在尝试使用 SwitchCompat,它显示在 Android Studio 的设计 View 中,但在我的 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>
构建.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 - 在您的 Activity 中使用 AppCompat 版本的小部件,例如。导入 android.support.v7.widget.SwitchCompat;
4 - 确保将 styles.xml 和 AndroidManifest.xml 中的主题设置为 AppCompat 主题之一。
5 - 如果您遇到错误,请尝试使缓存无效并重新启动,以便重置您的 R 资源。
关于android - SwitchCompat 未在设备上显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633310/