【问题标题】:Android Studio : Gradient background not showing in ButtonAndroid Studio:渐变背景未显示在按钮中
【发布时间】:2021-05-28 06:59:09
【问题描述】:

好的,所以我的按钮没有显示应有的渐变(渐变没有显示,它只是正常的颜色背景),我知道为什么会这样,一切看起来都应该是我的样子,有人可以接受吗看看:

activity_main.xml

...

   <Button
            android:id="@+id/button1"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:background="@drawable/button"
            app:icon="@drawable/ic_baseline_room_service_24"
            android:fontFamily="monospace"
            android:padding="15dp"
            android:paddingEnd="40dp"
            android:text="BROWSE BY INGREDIENTS"
            android:textColor="#FFFFFF"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/guideline2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.084"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/viewPager"
            app:layout_constraintVertical_bias="0.396" />
...

这是布局中显示的按钮照片: button (gradient not working)

button.xml

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

    <item>
        <shape android:shape="rectangle">

            <gradient
                android:startColor="#1746D3"
                android:endColor="#6D34CF"/>
            <corners
                android:bottomRightRadius="900dp"
                android:radius="90dp"/>
        </shape>
    </item>

</selector>

这是渐变背景的照片:gradient

但是,android:background="..." 确实在侧面显示了迷你预览:button_preview

如有任何帮助,我们将不胜感激!

【问题讨论】:

  • 您使用的是 Material 主题吗? Material Buttons 不支持渐变或自定义背景,它们只支持不同的颜色 backgroundTint
  • @TorkelVelure 不,我没有使用 Material Button... 没关系,@MarcoNardelotto 的答案对我有用,即将 Button 更改为 AppCompatButton

标签: android button gradient


【解决方案1】:

Button 中的androidx.appcompat.widget.AppCompatButton 替换为activity_main.xml 中的androidx.appcompat.widget.AppCompatButton

似乎使用 Button 无法完全个性化小部件。

Button 小部件处理自己的背景,您只能使用着色来更改其颜色。这是一个未解决的问题,尚未修复。

【讨论】:

    猜你喜欢
    • 2012-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2021-10-17
    相关资源
    最近更新 更多