android - 应用 BottomSheetDialogFragment 背景后无法更改按钮背景
问题描述
我的 BottomSheetDialogFragment 需要圆角,因此我应用了自定义可绘制对象作为其背景。但是在应用自定义背景后,BottomSheet 上的按钮不接受自定义背景。它只显示白色背景。backgroundTint 可以正常工作并更改按钮的颜色。
主题.xml:
<style name="AppBottomSheetDialogTheme"
parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/AppModalStyle</item>
</style>
<style name="AppModalStyle"
parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/add_new_bg</item>
</style>
add_new_bg.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/background"/>
<corners
android:topLeftRadius="40dp"
android:topRightRadius="40dp"/>
</shape>
BottomSheetDialogFragment 代码:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NORMAL, R.style.AppBottomSheetDialogTheme);
}
解决方案
尝试这个 :
- 如果您的主题是Theme.AppCompat.Light:
例子 :
<style name="Theme.YourAppName" parent="Theme.AppCompat.Light">
<!-- Primary brand color. -->
<!-- Customize your theme here. -->
</style>
你的按钮是:
<Button <------ material button type
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/buttonTheme" <---- add this
android:text="MyButton"/>
将此添加到主题下方的主题.xml 中
<style name="buttonTheme" parent="Theme.MaterialComponents.Light">
<item name="theme">@style/buttonTheme</item>
<item name="android:textColor">@color/white</item>
<item name="android:background">@color/black</item>
</style>
- 如果您的主题是:Theme.MaterialComponents
例子 :
<style name="Theme.RecyclerView" parent="Theme.MaterialComponents.Light">
<!-- Primary brand color. -->
<!-- Customize your theme here. -->
</style>
但你的按钮使用:
<androidx.appcompat.widget.AppCompatButton <<<<--- appcompact button
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/buttonTheme"
android:text="MyButton"/>
然后加 :
<style name="buttonTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="theme">@style/buttonTheme</item>
<item name="android:textColor">@color/white</item>
<item name="android:background">@color/black</item>
</style>
按钮类型的主题必须是相同的类型。
让我知道这是否适合您..并投票赞成:)
推荐阅读
- sql-server - SQL SERVER XQuery 节点处理问题
- opengl - OpenGL Compute 阶段与其他阶段
- ionic-framework - ionic 4,如何从 Livereload 去构建?
- html - 以html形式改变行之间的垂直间距
- python - icalendar AssertionError:VTIMEZONEs 子组件的 DTSTART 必须是 datetime 类型,而不是 date
- bash - 一个接一个的两个外壳参数扩展不起作用(Bash)
- laravel - 我在 lavavel 中有一个包含多个会话的数据集合,如何按会话分类显示它们
- ruby-on-rails - 刺激反射生命周期事件
- java - 有没有办法为我在 ignitecache 中插入的不同元素设置不同的到期时间?
- java - 如何在 Java 中将 BufferedImage 的四边形区域转换为矩形 BufferedImage?