0

Hi, I am working on custom keyboard tool in android it is somewhat same as our default android device keyboard. Below shows the format of my keyboard.

enter image description here

So, here i am setting backgorund color to button but it is looking very clumsy as shown below,

enter image description here

i need to set background color as red same as shown in above screenshot which is default color of button.It changes the dimension of button when i set background color.

Code i have used to set background color is,

int mColor = 0xFFFF0000;
b.setBackgroundColor(mColor);
v.setBackgroundColor(mColor);

So where i am going wrong.

4

2 回答 2

2

enter code here您的普通按钮从系统获得了设计。如果您更改颜色,则设计将更改为标准。

您应该为具有形状的按钮创建自己的样式

<?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="#aaaaaa"
        android:startColor="#555555" />

    <stroke
        android:width="1dp"
        android:color="#000000" />

    <corners
        android:bottomLeftRadius="10sp"
        android:bottomRightRadius="10sp"
        android:topLeftRadius="10sp"
        android:topRightRadius="10sp" />

</shape>

在您的drawable中创建一个xml文件并插入上面的代码。

您什么时候想更改按钮的颜色?如果你想通过点击改变颜色,你应该使用选择器,像这样

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/shapeButtonRed" android:state_focused="true"/>
    <item android:drawable="@drawable/shapeButtonRed" android:state_pressed="true"/>
    <item android:drawable="@drawable/shapeButton"/>

</selector>

现在你必须给你按钮,android:backgroud"@drawable/selector"它会通过点击改变颜色

于 2013-06-27T06:39:35.000 回答
1

您可以使用颜色过滤器来实现两个按钮的红色着色:

int mColor = 0xFFFF0000;
b.getBackground().setColorFilter(mColor, PorterDuff.Mode.MULTIPLY);
v.getBackground().setColorFilter(mColor, PorterDuff.Mode.MULTIPLY);

使用此导入语句时:

import android.graphics.PorterDuff;

这将保留按钮的默认设计。

于 2013-06-27T06:50:07.690 回答