android - 为按钮定义一个可绘制对象并根据所选的 MeterialDesign 主题对其进行更改
问题描述
我有一个 ImageButton,它使用 drawable/sampleimage.xml 作为源,我使用 MaterialDesign 作为我的应用程序主题。
我的按钮布局如下所示:
<ImageButton
android:id="@+id/sampleButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/sampleimage.xml" />
我的styles.xml 看起来像这样:
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
我使用 Android 10 中的设置或使用
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
和
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
在安卓 9 上。
我不知道如何为我的按钮定义单个可绘制对象,该按钮会根据所选主题而变化。我想将 drawable/sampleimage_light.xml 用于浅色主题,而在使用深色主题时使用 drawable/sampleimage_dark.xml。
解决方案
我的可绘制对象是矢量图像,所以我只是将填充颜色更改为 ?attr/colorOnSurface
我的drawable现在看起来像这样:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?attr/colorOnSurface"
android:pathData="M8,5v14l11,-7z"/>
</vector>
推荐阅读
- php - 用简单的 HTML Dom 解析 SVG
- grpc - 最大 gRPC 连接数基准
- c# - 如何获取路由属性 dotnet core 3?
- numpy - Pytorch:将 [1,x] 大小的张量转换为 [x] 大小的张量
- xamarin - ActivityProvider 不运行
- xamarin.forms - 如何从代码中将 Layout 绑定到 ViewModel?
- java - Sceneform:将 WebRTC 媒体流渲染到 ExternalTexture
- nginx - 无法使用 Nginx 访问在 80 以外的端口上运行的 server_block
- swift - 通配符模式匹配:
- typescript - 如何使用量角器验证网页中显示的图形?