【问题标题】:Bottom Sheet button custom style not working底部工作表按钮自定义样式不起作用
【发布时间】:2020-08-16 15:23:21
【问题描述】:

在我的BottomSheetDialogFragment 中,我有一个按钮,我想让这个按钮背景为灰色边框。

这是我的圆形背景可绘制“rounded_tranparent_background.xml”:

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle">
<solid android:color="#FFF" />
<stroke
    android:width="1dp"
    android:color="#8A8383" />
<corners
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp" />
 </shape>

我将此背景应用于底部的工作表按钮。

这是底部工作表布局代码:

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">


<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnFollowUnfollow"
        android:background="@drawable/rounded_tranparent_background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/avenir_next_ltpro_medium"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="Unfollow"
        android:textColor="@color/colorGreyDark"
        app:layout_constraintBaseline_toBaselineOf="@+id/tvTitle"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.71"
        app:layout_constraintStart_toEndOf="@+id/tvTitle" />

    //...other code...

 </androidx.constraintlayout.widget.ConstraintLayout>

 </layout>

这是我的底部工作表对话框样式

<style name="AppBottomSheetDialogTheme"
    parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="colorPrimary">@color/colorBlue</item>
    <item name="colorPrimaryDark">@color/colorBlueDark</item>
    <item name="colorAccent">@android:color/transparent</item>
</style>

应用所有这些代码后,我无法实现我想要的。 我想要的设计是:

我得到了什么

如何在底部工作表对话框中的按钮上实现我的自定义设计?

【问题讨论】:

  • 你为什么使用 。相反,您应该只使用 topLeft 和 topRight 。
  • 另外,您可以使用自定义 UI 来“关闭”。使用 ViewObject(关闭)下方的视图。给背景黑色,它会类似于上面的例子
  • 你在使用材料组件库吗?

标签: android android-button android-styles android-dialogfragment android-bottomsheetdialog


【解决方案1】:

你可以使用简单的MaterialButton:

        <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cornerRadius="10dp"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:textColor="..."
            app:strokeColor="..."/>

【讨论】:

  • 一个关键说明是小部件主题定义在应用程序主主题中全局设置被BottomSheetDialogFragment忽略,所以我们必须在小部件的xml定义中明确设置所需的主题(如答案)。
【解决方案2】:

例如,您可以为按钮使用自定义可绘制对象。

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:ignore="ResourceName">
    <solid android:color="@color/inside_color" />

    <stroke
        android:color="@color/border_color"
        android:width="3px" />
    <corners android:radius="16dp" />

</shape>

输出会是这样的

【讨论】:

  • 如果您检查我的问题,这部分代码已经存在。我认为问题出在bottomSheetDialogFragment背后。
  • 很遗憾,兄弟,你没有写这个代码或者你没有上传它。我还没有看到你的drawable。相反,您使用的是 style = "btnFollowUnFollow"。您应该使用其中之一。不是他们两个。它不起作用,因为你有悲伤。将样式更改为可绘制,然后按钮将更改。
  • 检查问题的第一部分,我不知道我的代码和你的有什么不同......
【解决方案3】:

这个修复对我有用:

<Button        
    android:background="@drawable/rounded_tranparent_background"
    ...

同时添加 "android:backgroundTint" 和 app:backgroundTint:

<Button        
    android:background="@drawable/rounded_tranparent_background"
    android:backgroundTint="@null"
    app:backgroundTint="@null"
    ...

BottomSheetDialogFragment 中也忽略了我的按钮主题定义

<-- Themes.xml -->
<style>
    <item name="materialButtonStyle">MyButtonStyle</item>

<!-- So quick fix: -->
<Button        
     android:background="@drawable/rounded_tranparent_background"
     android:theme="@style/MyButtonStyle"

【讨论】:

    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-12
    相关资源
    最近更新 更多