【问题标题】:Cannot change Button background after applying BottomSheetDialogFragment background应用 BottomSheetDialogFragment 背景后无法更改按钮背景
【发布时间】:2021-02-14 00:47:00
【问题描述】:

我的 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);
    }

【问题讨论】:

    标签: android android-studio android-bottomsheetdialog bottomsheetdialogfragment


    【解决方案1】:

    试试这个:

    1. 如果您的主题是 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>
    
    1. 如果您的主题是: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>
    

    按钮类型的主题必须是同一类型。

    让我知道这是否适合你.. 并支持它:)

    【讨论】:

    • 不工作 :-(。按钮仍然有白色背景
    • 检查我的新答案
    • 背景仍然没有改变。如果我使用 Theme.MaterialComponents.DayNight.NoActionBar 作为我的父主题,按钮是白色的。但是当我使用 Theme.AppCompat.Light 时这是正常的。
    • 是的,所以使用选项 2
    【解决方案2】:

    查看您是否只想将背景颜色应用于底部工作表中的按钮 只需在代码中使用背景色“YOUR_COLOR_CODE”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 2020-01-01
      • 1970-01-01
      相关资源
      最近更新 更多