Android:使用定义的可绘制对象以编程方式更改按钮颜色

标签 android

我有:

public Button[] getButtons(int buttonCount, List<String> buttonHeaders, Context activity) {
    Button[] result = new Button[buttonCount];

    for (int i = 0; i < buttonCount; i++) {

        result[i] = new Button(activity);

        result[i].setId(i);
        result[i].setText(buttonHeaders.get(i));
        result[i].setBackgroundResource(R.drawable.buttons_shape);
        result[i].setBackgroundColor(randomColor(activity));
        result[i].setTextColor(activity.getResources().getColor(R.color.white));
        result[i].setWidth(360);
        result[i].setHeight(100);
    }
    return result;
}

但在我的可绘制对象中是:

 <solid android:color="@color/white"/> 
    <corners
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp"/>

但是我想给程序定义什么颜色,因为颜色是随机的,当我改变背景时又回到默认设计(PS。对不起英文:P)

最佳答案

你不能两者都做..要么你可以做 view.setBackgroundDrawable(drawable);或 view.setBackGroundColor(color);

关于Android:使用定义的可绘制对象以编程方式更改按钮颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17700096/

相关文章:

android - 从 PlaceAutoComplete 搜索城市名称会给出加密的城市名称

android - Gradle错误:错误:任务 ':app:dexDebug'的执行失败

java - app-debuk.apk停止工作

android - libgdx Viewports - 为什么当我使用 FitViewport 时触摸输入 x 和 y 不准确?

java - Android中的Django JsonResponse解析错误

android - 如何使用 ScnViewGestures.Forms 在 Xamarin.Forms 中创建包含 TouchDown 和 TouchUp 事件的 View

java - Eclipse ADT 22.6.2 仍然给出错误 "java.lang.NullPointerException"

android - 如何在Android中设置可靠的长期闹钟?

Android SSL 证书固定与改造

android - 蓝牙 RFCOMM/SDP 连接到 Android 中的 RS232 适配器