Android:为按钮设置自定义渐变
如何设置我的Android:为按钮设置自定义渐变,android,Android,如何设置我的渐变图像作为按钮的背景。我看到属性渐变,但看不到任何包含背景的属性 注意:我是非常新的Android开发人员。您应该用XML定义渐变,或者使用图像(包括圆角)。您不能轻松地将XML形状和图像混合在一起(至少,作为初学者,我建议您先使用简单的东西) 例如: <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"
渐变图像
作为按钮的背景。我看到属性渐变,但看不到任何包含背景的属性
注意:我是非常新的Android开发人员。您应该用XML定义渐变,或者使用图像(包括圆角)。您不能轻松地将XML形状和图像混合在一起(至少,作为初学者,我建议您先使用简单的东西) 例如:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EAEAEA"/>
<corners android:bottomLeftRadius="5dip"
android:topRightRadius="5dip"
android:topLeftRadius="5dip"
android:bottomRightRadius="5dip"
/>
</shape>
然后,您可以使用android:background=“@drawable/bg\u custom\u button”
您应该了解九个补丁,它们允许您为您的背景定义可拉伸的图像,并在设计不适用于XML时为您节省时间。我不确定您向我们展示的XML与渐变有什么关系。您可以在
drawable
文件夹中的XML文件中定义渐变:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#474946"
android:endColor="#181818"
android:angle="270"/>
<corners android:radius="5dp" />
</shape>
您的形状方向正确,但可以使用渐变来代替实体
<Button android:id="@+id/ButtonStart"
android:layout_width="100dp" android:layout_height="wrap_content"
android:background="@drawable/my_gradient"
android:textColor="@color/white" android:textSize="14sp"
android:textStyle="bold" android:text="@string/game_start"/>
假设上面的形状保存为gradient_background.xml,并且您将其保存在drawable文件夹中(应该在该文件夹中)。现在,您可以使用此可绘制按钮作为背景
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="@color/gradient_bottom"
android:startColor="@color/gradient_top" />
<corners android:bottomLeftRadius="5dip"
android:topRightRadius="5dip"
android:topLeftRadius="5dip"
android:bottomRightRadius="5dip"
/>
</shape>
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="270"
android:endColor="@color/gradient_bottom"
android:startColor="@color/gradient_top" />
<corners android:bottomLeftRadius="5dip"
android:topRightRadius="5dip"
android:topLeftRadius="5dip"
android:bottomRightRadius="5dip"
/>
</shape>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gradient_background"
android:text="Button" />