简体   繁体   English

我可以改变颜色吗?

[英]Can I change the color?

I am using the following code to make a star button: 我正在使用以下代码制作一个星形按钮:

<Button
    android:id="@+id/leftButton"

    android:onClick = "Star"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:Color="#FFFF00"   //I tried adding this, but no luck :(
    android:layout_alignParentTop="true"
    android:background="@android:drawable/btn_star" />

The button currently looks white: 该按钮当前显示为白色:

在此处输入图片说明

Is there anyway I can make it yellow? 反正我能把它变黄吗?

I know I can't have two background attributes, but is there anything I can do to change the color of this star? 我知道我不能具有两个背景属性,但是我可以做些什么来改变这颗星星的颜色吗? I want to keep the star as a background attribute, rather than a source attribute, because I want to be able to change the size of it. 我希望将星形保留为背景属性,而不是源属性,因为我希望能够更改其大小。

If you want to yellow the star icon, then you can use ColorFiler for this. 如果要使星形图标变黄,则可以使用ColorFiler As follows: 如下:

Drawable drawable = getResources().getDrawable(android.R.drawable.star);
drawable.setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);

Hope this helps. 希望这可以帮助。

Not directly. 不直接。 You can add different colorized images and change them at runtime, but much better way is to use a xml drawable. 您可以添加不同的彩色图像并在运行时进行更改,但是更好的方法是使用xml drawable。 For icons with xml files take a look at https://materialdesignicons.com (there are much more sites like that outside) 对于带有xml文件的图标,请访问https://materialdesignicons.com (外面有更多类似的网站)

In the xml file you can change the color directly 在xml文件中,您可以直接更改颜色

Your xml file. 您的xml文件。

 <Button
        android:id="@+id/leftButton"
        android:tint="@color/yellow_tint"
        android:onClick = "Star"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:background="@android:drawable/btn_star" />

colors.xml colors.xml

<resources>

 <color name="yellow_tint">#FFFF00</color>

</resources>

Try it and let me know. 试试看,让我知道。 Refer to this: Working with Drawables . 请参考: 使用可绘制对象 I'm afraid android:color won't work in Drawables. 恐怕android:color在Drawables中不起作用。

(background_star.xml) on drawable folder: (background_star.xml)在可绘制文件夹中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/star_yellow" <!-- image yellow -->
        android:state_pressed="true" />
    <item android:drawable="@drawable/star_normal" /> <!-- image normal-->
</selector>

on layout file: 在布局文件上:

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancel alarm"
        android:id="@+id/button2"
        android:background="@drawable/background_star" <!-- change this -->
        android:layout_centerHorizontal="true" />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM