2016-01-25 50 views
-1

我使用下面的代码,以使一个星号按钮:我可以改变颜色吗?

<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" /> 

按钮目前看起来是白色:

enter image description here

反正我有可以令它发黄?

我知道我不能有两个背景属性,但有什么我可以做的,以改变这颗恒星的颜色?我希望将星形保留为背景属性,而不是源属性,因为我希望能够更改它的大小。

+0

我真的很感激任何帮助,因为我一直在试图找出了这一点现在一个多小时! –

+1

你怎么决定什么时候显示黄色版本? – Blackbelt

+0

你想改变颜色为黄色onclick?或者只是一个静态的黄色图标?如果是静态的,为什么不用黄色的替换可绘制的图标? –

回答

0

不是直接。您可以添加不同的彩色图像并在运行时更改它们,但更好的方法是使用xml drawable。对于XML文件的图标看看https://materialdesignicons.com(也有更多的网站像外)

在XML文件中,你可以改变颜色直接

2

如果你想黄色星形图标,那么你就可以为此使用ColorFiler。如下:

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

希望这会有所帮助。

+0

什么是'PorterDuff.Mode.SRC_ATOP' –

+0

这是什么意思?谢谢! –

+0

请通过链接http://ssp.impulsetrain.com/porterduff.html,它有不同的porterduff模式很好的解释。 –

0

您的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

<resources> 

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

</resources> 

试试吧,让我知道。参考:Working with Drawables。我很害怕android:color在Drawables中不起作用。

+0

没有工作...白色... –

0

上绘制文件夹(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> 

上布局文件:

<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" /> 
+0

使用ImageView的方形图像... – Lucas

+0

无法解析符号'star_yellow' ... –

+0

卢卡斯似乎建议您为此添加其他资产。 – Barett

相关问题