android - Android Material Button - @drawable specs not recognized
问题描述
New to AS. Trying to use MaterialButton, overriding default background with @drawable, but it's not working.
Environment Android Studio: 4.0 Dependencies: implementation "androidx.appcompat:appcompat:$dependency_version_appcompat" implementation "com.google.android.material:material:$dependency_version_material"
Drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shape="rectangle">
<solid
app:backgroundTint ="?attr/ColorBackgroundAppBar">
</solid>
<stroke
android:width="1px"
android:color="?attr/ColorBorderDark">
</stroke>
</shape>
Layout
<com.google.android.material.button.MaterialButton
android:id="@+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background = "@drawable/common_appbar_btn_background"
android:elevation="@dimen/common_elevation_height_btn"
... />
With the above, I just get the default MaterialButton background.
If I set backgroundTint to @null
<com.google.android.material.button.MaterialButton
android:id="@+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
app:backgroundTint="@null"
android:background = "@drawable/common_appbar_btn_background"
android:elevation="@dimen/common_elevation_height_btn"
... />
With the above, the drawable specs are recognized, but if I change the theme style, that doesn't work.
These are the only settings that work:
<com.google.android.material.button.MaterialButton
android:id="@+id/common_appbar_btn_lang_mode"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:backgroundTint = "?attr/ColorBackgroundAppBar"
app:strokeColor="?attr/ColorBackgroundAppBar"
app:strokeWidth="0px"
android:elevation="@dimen/common_elevation_height_none"
android:fontFamily="@font/font_family_kaiti"
android:text="简"
android:textAlignment="center"
android:textColor="?attr/ColorTextPrimary"
android:textStyle="normal"
android:textFontWeight="700"
android:textSize="24dp" />
Am I doing sth wrong...or is MaterialButton not quite there yet? I can live with the last solution, but it increases maintenance...
解决方案
使用以下方式定义您的应用主题Theme.MaterialComponents.DayNight
:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryVariant">@color/colorPrimaryLight</item>
<item name="colorSecondary">@color/colorSecondary</item>
<!-- .... -->
</style>
然后,如果需要,定义明暗模式的颜色:
示例res\values\colors.xml
::
<color name="colorPrimary">.....</color>
在res\values-night\colors.xml
文件夹中定义相同的颜色:
<color name="colorPrimary">.....</color>
最后为您的按钮定义一个样式(对于深色和浅色模式,您不需要 2 种不同的样式):
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@color/....</item>
<item name="strokeColor">@color/....</item>
<item name="strokeWidth">@dimen/....</item>
</style>
最后一步将样式分配给布局中的按钮。
<com.google.android.material.button.MaterialButton
style="@style/Widget.App.Button"
.../>
或在您的应用主题中全局:
<item name="materialButtonStyle">@style/Widget.App.Button</item>
由于Button
您没有使用渐变,因此您可以使用MaterialButton
.
如果您更喜欢将android:background
属性与自定义可绘制对象一起使用,您可以使用:
<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@null</item>
<item name="android:background">@drawable/.....</item>
</style>
然后在可绘制对象中使用应用程序主题res\values\colors.xml
和res\values-night\colors.xml
文件夹中定义的颜色或属性。
推荐阅读
- css - Yii2 Gridview - 水平顶部滚动条
- python-3.x - 如何使用机器人框架向命令提示符发送命令?
- c++ - 如何将字符串数组传递给 QtConcurrent::run?
- javascript - 如何实现重复的承诺调用链?
- sql - SQL Server:如何将 int 的值转换为 hh:mm
- mysql - SQLite 结果与 MySQL 结果不同
- ios - 如何使用特定用户的 uid 获取特定用户的数据?
- python - 获取数据框中每一列的差异并在完成后重新排序的最有效方法
- python - 合并同一数据框中的重复列
- html - iframe 无法在 Firefox 中打开 pdf 文件“无法预览”