【问题标题】:Why is my drawable button layout not being applied to the button as a background?为什么我的可绘制按钮布局没有作为背景应用于按钮?
【发布时间】:2021-07-19 16:10:07
【问题描述】:

我创建了一个 buttonbg.xml 文件,为按钮提供自定义颜色的圆形。 这是xml代码的样子,

<stroke
    android:color="#69B9F9"
    android:width="3dp" />

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

但是当我将此 xml 作为背景应用到按钮时,

<Button android:id="@+id/loginButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentBottom="true"
    android:layout_marginStart="59dp"
    android:layout_marginEnd="61dp"
    android:layout_marginBottom="227dp"
    android:background="@drawable/button_bg"
    android:text="LOGIN"
    android:textSize="22sp" />

什么都没发生,

Button Image

请帮帮我!

【问题讨论】:

    标签: android xml button layout


    【解决方案1】:

    将此添加到您的按钮

    app:backgroundTint="@null" 
    

    你也写过你的问题

    我已经创建了一个 buttonbg.xml 文件

    在您的代码中,您已经编写了 button_bg.xml。请检查您的代码中是否存在拼写错误或只是在问题本身中

    【讨论】:

      【解决方案2】:

      您可以创建按钮可绘制对象并将该可绘制对象应用于线性布局或任何其他布局。所以基本上创建自定义按钮是实现自定义按钮的非常简单的方法。

      【讨论】:

        【解决方案3】:

        当您不想在每次想为按钮添加背景时都使用app:backgroundTint="@null 时,这里有一个提示。转到您的 themes.xml 文件。在第一个 style 标记中,您会看到一些内容

        <style name="Theme.[AppName]" parent="Theme.MaterialComponents...."
        

        所以将 parent 更改为 "Theme.AppCompat.Light.DarkActionBar" (如果您不介意顶部的操作栏您的应用名称)或 "Theme.AppCompat.Light.NoActionBar"(如果您不想要操作栏。

        因此,它的作用是删除默认按钮样式,并且只为您提供一个纯灰色按钮,如果您从那里学习,您可能已经在 youtube 上的一些教程中看到过。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-08
          • 1970-01-01
          相关资源
          最近更新 更多