【问题标题】:Background drawable not applying to button背景可绘制不适用于按钮
【发布时间】:2019-12-21 07:16:42
【问题描述】:

我正在尝试在 XML 中创建一个胶囊/药丸形状的按钮。我已经定义了一个drawable并将其设置为我的按钮的背景,但是在预览中,当我运行应用程序时,它显示为一个蓝色矩形,尽管背景drawable是一个白色的椭圆形。有谁知道为什么会发生这种情况?

这是按钮:

<Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/search_box"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="24dp"
            android:background="@drawable/button_capsule"
            android:text="@string/search"
            android:textColor="@color/precipLightBlue"/>

这是背景可绘制对象:

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="1000dp" />

    <solid android:color="@android:color/white" />

</shape>

【问题讨论】:

  • 您可以添加图片吗?这样我们就可以了解其中的实际问题是什么

标签: android android-layout material-design android-button material-components-android


【解决方案1】:

要拥有胶囊/药丸形状的按钮,您可以使用MaterialButton 官方Material Component library 中的app:cornerRadius 属性。

 <com.google.android.material.button.MaterialButton
            android:layout_width="100dp"
            app:cornerRadius="32dp"
            ..../>

在 1.1.0 版本中,您还可以使用 app:shapeAppearanceOverlay 属性 customize the shape 您的组件

<com.google.android.material.button.MaterialButton
     ....
     app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.ButtonRounded"
     .../>

在样式定义中:

 <style name="ShapeAppearanceOverlay.ButtonRounded" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">32dp</item>
 </style>

您也可以尝试使用ExtendedFloatingActionButton

<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/exfab"
        android:layout_width="wrap_content"
        .... />

【讨论】:

    【解决方案2】:

    您放置了一个未定义的形状。要定义椭圆,您应该将 shape="oval" 选项放在标签中。虽然我认为你想要一个圆角矩形,正如我在你的代码中看到的那样。

    1000dp 的半径也很大,这可能是个问题。 也定义大小。因为该形状没有任何大小,并且在您在按钮定义中使用 wrap_content 时可能不会出现

    试试这个:

    <corners android:radius="30dp" />
    
    <solid android:color="@android:color/white" />
    
    <size
        android:width="200dp"
        android:height="50dp"/>
    

    如果你想要一个椭圆,删除角标签并添加 android:shape="oval" 作为形状标签的属性

    【讨论】:

      【解决方案3】:

      这是关于按钮可绘制背景的代码。

      Xml 代码

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:gravity="center|top"
      android:background="@color/colorLightPurple">
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="24dp"
          android:background="@drawable/button_bg"
          android:text="Search"
          android:textColor="@color/colorBlue"/>
      </LinearLayout>
      

      对于按钮背景,请添加相同的可绘制文件

      【讨论】:

        【解决方案4】:

        看起来你做的一切都是对的。只是一个小补充,尝试在形状标签中添加android:shape="rectangle"

        button_capsule.xml 应该是这样的。

        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <corners android:radius="1000dp" />
            <solid android:color="@android:color/white" />
        </shape>
        

        希望这可行。一切顺利。

        【讨论】:

          【解决方案5】:

          Material design version 1.2.0中,他们已经修复了这个问题并添加了一个属性来设置Material Button的背景 这是最新版本的依赖项

          com.google.android.material:material:1.2.0
          

          如果你想删除可绘制顶部和底部之间的空间,以便可绘制获得整个宽度和高度,请使用这 2 个属性

              android:insetTop="0dp"
              android:insetBottom="0dp"
          

          如果想了解更多关于属性的信息,你可以参考库的发布,并可以检查所有属性。

          https://github.com/material-components/material-components-android/releases/tag/1.2.0

          【讨论】:

            【解决方案6】:

            使用这个。

            【讨论】: