gpt4 book ai didi

Android:以编程方式 ImageButton 的非按下状态颜色

转载 作者:行者123 更新时间:2023-11-29 21:03:58 24 4
gpt4 key购买 nike

目前ImageButton设置了android:background="@drawable/mem_btn",

mem_btn.xml

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

<item android:state_pressed="true" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="@color/black" />
<solid android:color="@color/pressed"/>
<padding android:left="5dp" android:top="5dp"
android:right="5dp" android:bottom="5dp" />
<corners android:radius="14dp" />
</shape>
</item>

<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="0dp" android:color="@color/black" />
<solid android:color="#3366CC"/>
<padding android:left="5dp" android:top="5dp"
android:right="5dp" android:bottom="2dp" />
<corners android:radius="14dp" />
</shape>
</item>

</selector>

以上内容完美无缺。

问题:

我想让用户更改背景颜色:即根据用户选择的其他颜色,但使用 pressed_color 时,半径保持不变。

在这种情况下,如何以编程方式设置 xml 中的信息,使非按下状态的颜色成为一个变量?

谢谢!

最佳答案

感谢 Ranjit Pati 的引用,让我可以在正确的轨道上进一步研究并找到 StateListDrawable,并且以下工作完美:

public void set_buttons(int t, int color_idd) 
{
ShapeDrawable activeDrawable = new ShapeDrawable();
ShapeDrawable inactiveDrawable = new ShapeDrawable();

// The corners are ordered top-left, top-right, bottom-right, bottom-left. // For each corner, the array contains 2 values, [X_radius, Y_radius]

float[] radii = new float[8];
for (int i = 0; i <= 7; i++)
{
radii[i] = (int) getResources().getDimension(R.dimen.footer_corners);
}
inactiveDrawable.setShape(new RoundRectShape(radii, null, null));
inactiveDrawable.getPaint().setColor(color_idd);

activeDrawable.setShape(new RoundRectShape(radii, null, null));
activeDrawable.getPaint().setColor( (Color.parseColor ("#008B00")));

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {-android.R.attr.state_enabled}, inactiveDrawable);
states.addState(new int[] {android.R.attr.state_pressed}, activeDrawable);
btns[t].setBackground(states);
}

关于Android:以编程方式 ImageButton 的非按下状态颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25105693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com