为什么SwitchCompat在Android Studio上无法工作?

3

为什么SwitchCompat不起作用,并且有这个灰色矩形形状?我认为我已经尝试了几乎所有的东西,但它仍然不想工作。

Gradle文件:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ustawienia">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"/>

<include layout="@layout/content_ustawienia" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_animacja"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"/>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

你必须使用 androidx.appcompat.widget.SwitchCompat。不要混合使用 androidx 和 support 库。 - Gabriele Mariotti
1个回答

6

与其

android.support.v7.widget.SwitchCompat

使用

androidx.appcompat.widget.SwitchCompat

除此之外,您正在混合使用support libraryAndroidX library。请尝试删除support library,因为它们已过时。

如果您的项目没有干净地迁移到AndroidX,请通过从Android Studio菜单栏选择Refactor > Migrate to AndroidX,然后按Do Refactor进行迁移。

有关详细信息,请查看官方文档


1
@willie244,请点赞以便其他人能够找到它有帮助。 - Md. Asaduzzaman
好的。它运行正常!你知道怎样使这个“on click”灰色变成白色或透明吗?https://i.imgur.com/gV24o9c.png - user12398554
嗨@willie244,这是一个独立的问题。请在单独的线程中提出以保持清洁。希望你能理解。我们在这里为你提供帮助。谢谢。 - Md. Asaduzzaman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接