在 gradle 中添加 appcompat-v7 不会自动将我的布局视图切换到 AppCompat 版本

Adding appcompat-v7 in gradle is not switching my layout views to AppCompat versions automatically

我需要在 pre-lollipop 按钮和 post-lollipop 按钮中具有相同的外观,并且我正在使用 colorButtonNormal 属性修改按钮颜色。

我正在为我的应用程序使用这个主题:<style name="AppTheme" parent="Theme.AppCompat.Light">

因为我需要在所有 android 版本中使用相同的外观,所以我在 gradle 中这样做:

implementation 'com.android.support:appcompat-v7:28.0.0'

如文档中所述,只需添加该行即可将 xml 布局中的所有小部件转换为这些小部件的 appcompat 版本,以显示与旧 android 版本相同的外观在现代 android 版本中。

Button 的 AppCompat 版本必须能够访问 xml 中的此属性:

<Button
    app:backgroundTint="@color/unpressed2">
</Button>

但是由于忽略了它,所以出了点问题。如果我将 xml 小部件强制设为 AppCompat 版本,它会完美运行:

<android.support.v7.widget.AppCompatButton
    app:backgroundTint="@color/unpressed2">
</android.support.v7.widget.AppCompatButton>

为什么添加实现 'com.android.support:appcompat-v7:28.0.0' 不会自动将我所有的 Button 小部件更改为 android.support.v7.widget.AppCompatButton 小部件?

您需要使用 AppCompatActivity 才能在 xml 布局中使用 AppCompat widgets