<?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>如何将gradient image设置为按钮的背景。我看到一个属性梯度,但不能看到在其中包含背景的任何属性。
Note:我是一个非常新的安卓开发人员。
发布于 2012-10-18 13:56:52
我不知道您向我们展示的XML与渐变有什么关系。可以在drawable文件夹中的XML文件中定义渐变:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FFFFFFFF"
android:endColor="#FFD9D9D9"
android:angle="270"
/>
<corners android:bottomLeftRadius="5dip"
android:topRightRadius="5dip"
android:topLeftRadius="5dip"
android:bottomRightRadius="5dip"
/>
</shape>(例如,将其保存为my_gradient.xml)
然后,在布局xml文件中可以拥有:
<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"/>发布于 2012-10-18 13:55:50
您应该在XML中定义渐变,或者使用图像(包括圆角)。您不可能轻松地将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>然后,您可以使用android:background="@drawable/bg_custom_button"定义按钮的背景
您应该了解九个补丁,它们允许您为您的背景定义可维护的图像,并且在XML设计不可行时可以节省您的精力。
发布于 2012-10-18 13:59:31
你的形状是在正确的方向,但你可以用梯度代替固体。
<?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>假设上面的形状被保存为gradient_background.xml,并将其保存在可绘制文件夹中(应该在哪里)。您现在可以使用此绘图作为按钮的背景。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/gradient_background"
android:text="Button" />https://stackoverflow.com/questions/12956305
复制相似问题