我尝试使用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'
}
答案 0 :(得分:1)
这就是我最终实现它的方式:
1 - 下载Android支持存储库
2 - 添加xmlns:app =&#34; http://schemas.android.com/apk/res-auto"到您的活动XML
3 - 在您的活动中使用小部件的AppCompat版本,例如。 import android.support.v7.widget.SwitchCompat;
4 - 确保将styles.xml和AndroidManifest.xml中的主题设置为其中一个AppCompat主题。
5 - 如果您遇到错误,请尝试使缓存无效并重新启动,以便重置R资源。