首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android:为按钮设置自定义梯度

Android:为按钮设置自定义梯度
EN

Stack Overflow用户
提问于 2012-10-18 13:51:48
回答 3查看 2.2K关注 0票数 0
代码语言:javascript
复制
<?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:我是一个非常新的安卓开发人员。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-10-18 13:56:52

我不知道您向我们展示的XML与渐变有什么关系。可以在drawable文件夹中的XML文件中定义渐变:

代码语言:javascript
复制
<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文件中可以拥有:

代码语言:javascript
复制
<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"/>
票数 3
EN

Stack Overflow用户

发布于 2012-10-18 13:55:50

您应该在XML中定义渐变,或者使用图像(包括圆角)。您不可能轻松地将XML形状与图像混合(至少,由于您是初学者,我建议您首先使用简单的内容)。

例如:

代码语言:javascript
复制
<?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设计不可行时可以节省您的精力。

票数 2
EN

Stack Overflow用户

发布于 2012-10-18 13:59:31

你的形状是在正确的方向,但你可以用梯度代替固体。

代码语言:javascript
复制
<?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,并将其保存在可绘制文件夹中(应该在哪里)。您现在可以使用此绘图作为按钮的背景。

代码语言:javascript
复制
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/gradient_background"
    android:text="Button" />
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12956305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档