简体   繁体   English

如何通过xml为android中的按钮添加渐变?

[英]How to add a gradient to buttons in android through xml?

I cannot figure out why this code will not work.我无法弄清楚为什么此代码不起作用。 Can anyone help?任何人都可以帮忙吗? I am trying to get the button to use a custom color titled 'greenstart' and 'greenend'.我试图让按钮使用名为“greenstart”和“greenend”的自定义颜色。 The colors have been created in the res/value/string.xml file.颜色已在 res/value/string.xml 文件中创建。 I have looked at similar questions but either they were left unanswered, unclear, or the solution did not work.我看过类似的问题,但要么没有回答,要么不清楚,要么解决方案不起作用。 Thanks in advance.提前致谢。

XML FILE SAMPLE: XML 文件示例:

<Button
   android:id="@+id/mycollection"
   android:layout_width="match_parent"
   android:layout_height="fill_parent"
   android:layout_weight="1" >

   <Gradient
      android:angle="270"
      android:endColor="@color/greenstart"
      android:startColor="@color/greenend" >
   </Gradient>
</Button>

Create a new xml file and put it in drawable and then add it to button as background创建一个新的 xml 文件并将其放入 drawable 中,然后将其添加到按钮作为背景

gradient.xml渐变.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
    <!--  Gradient Bg for listrow -->
   <gradient
      android:startColor="#f1f1f2"
      android:centerColor="#e7e7e8"
      android:endColor="#cfcfcf"
      android:angle="270" />
</shape>

layout.xml布局文件

 <Button
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:background="@drawable/gradient"
    android:text="Übernehmen" >

Try this :试试这个 :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#70c656" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

So here we go with the gradient.所以在这里我们使用渐变。 As above @Dusean Singh said.如上所述@Dusean Singh 说。 If you will use an angle 270 then your gradient will start from top to down : TOP -> Center -> bottom如果您将使用角度 270,那么您的渐变将从上到下开始:顶部 -> 中心 -> 底部

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FF0000"
        android:centerColor="#00FF00"
        android:endColor="#0000FF"
        android:angle="270" />
</shape>

在此处输入图片说明

If you will use an angle 360 then your gradient will start from left to right : Left -> Center -> Right如果您将使用 360 度角,那么您的渐变将从左到右开始:左 -> 中心 -> 右

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--  Gradient Bg for listrow -->
    <gradient
        android:startColor="#FF0000"
        android:centerColor="#00FF00"
        android:endColor="#0000FF"
        android:angle="360" />
</shape>

在此处输入图片说明

Here we go with the effect.下面我们来看看效果。 and how to apply the same on button以及如何在按钮上应用相同的内容

<LinearLayout
    android:id="@+id/design_bottom_sheet"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">
    <Button
        android:drawableLeft="@drawable/ic_grid"
        android:layout_width="match_parent"
        android:text="Find"
        android:background="@drawable/gradient_button"
        android:textColor="@color/white"
        android:textAllCaps="false"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <Button
        android:drawableLeft="@drawable/ic_card"
        android:layout_width="match_parent"
        android:textColor="@color/white"
        android:text="Match"
        android:background="@drawable/gradient_button"
        android:textAllCaps="false"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

在此处输入图片说明

Create gradient.xml创建gradient.xml

<?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/greenstart"
      android:startColor="@color/greenend" />

</shape>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式在 Android 中的现有 xml 中添加按钮 - How to add buttons programmatically in an existing xml in Android 在android中动态添加按钮,如何将onClick添加到xml - Dynamically adding buttons in android, how to add onClick to xml 如何在 color 中添加渐变色。 xml - How to add the gradient color in color . Xml Android:如何以编程方式将按钮添加到布局(在 XML 视图中可以看到按钮) - Android: How to add Buttons to a Layout programmatically(with Buttons to be seen in the XML-View) 如何在Android中将src属性中的图像添加到gradient.xml文件? - How can I add image from src attribute to gradient.xml file in Android? Android多个渐变XML - Android multiple gradient xml 如何在Android中通过XML在可绘制图像上添加模糊效果 - How to add a blurry effect on drawable image through XML in android Android:使用xml drawable和gradient将类似边框的框架添加到ImageView - Android: Add frame like border to ImageView with xml drawable and gradient 如何通过Java文件android将其添加到XML数组中? - How to add to your array in xml through your java file android? 如何在Android上的Button上设置图像并通过xml添加圆角边缘? - How to set image on Button and add rounded edges through xml on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM