开发者

Why changing Color of Default Button makes it look RECTANGLE in Android?

Why changing Color of Default Button makes it look RECTANGLE shape ? I do not want to use custom background images for this. I want to do this programmatically for few conditions on which I change the colors of many small buttons on screen. Can anyone give a solution ?

P.S. ==> It seems there is no workaround by reading this http://groups.google.com/group/android-beg开发者_Go百科inners/browse_thread/thread/e1313e2c98e9c52b

or is there any ?


For changing color :

Drawable d = findViewById(R.id.button_name).getBackground();
               PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
               d.setColorFilter(filter);

For removing color :

Drawable d = findViewById(R.id.button_name).getBackground();
               findViewById(R.id.button_name).invalidateDrawable(d);
               d.clearColorFilter();


You can use the following XML script instead of Assiging colors as backgroud. First of all let me tell you that, the Button view by default is a rectangle so when you just add an button view in your layout, it is having a default background style assigned and because of that you can see it with rounded corners. when you assign the backgroud, the default stype/theme will be overridden and you will get the button in the original form so here you have to apply the theme/stype as per your requirement. If you want it for few buttons then you can use the following XML scrupt. YOu just create an .xml file in your drawable under resources in android project. then assign that .XML file as background of your button view. And see what happens...

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <corners android:radius="6dp"/>
    <padding android:left="3dp" android:top="2dp"
            android:right="3dp" android:bottom="2dp" />
</shape>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜