【发布时间】:2016-01-03 16:54:23
【问题描述】:
我有 48 个Button 的活动,用户可以触摸并更改文本和背景颜色。
但是当用户更改背景颜色时,我得到了这个糟糕的结果
这些是设置默认 Buttons 样式的 xml
buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused"
android:state_focused="true" />
<item android:drawable="@drawable/button_default" />
</selector>
button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="100dp"
/>
<solid
android:color="#FFFFFF"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<stroke
android:width="3dp"
android:color="#787878"
/>
</shape>
在其他 xml 中仅更改颜色,因此我避免发布它们。
这是以编程方式更改Button的代码。
我从DB 中取出所有已更改的Button,并使用ID 保存并设置Button 的颜色。
//Get all materie inside database
List<Materia> materia = db.getAllMaterie();
//change all TextView inputed from user
if(materia.isEmpty()){
//do nothing
}else {
for (Materia mat : materia) {
//Change all the Button with values stored inside the database
int resId = getResources().getIdentifier(mat.getID(), "id", getPackageName());
final Button changedButton = (Button) findViewById(resId);
changedButton.setText(mat.getMateria());
changedButton.setTypeface(null, Typeface.BOLD);
changedButton.setBackgroundColor(mat.getColor());
}
}
但是我失去了半径和描边属性。 有一些方法可以以编程方式设置它们吗? 接受其他建议!
【问题讨论】: