我有48 Button这个活动,用户可以触摸并更改文本和背景颜色。Android如何以编程方式设置按钮描边和半径
我已经编辑默认Buttons的风格像这样
但是,当用户改变背景颜色,我有这种坏的结果
这些都是xmls风格的默认Buttons
buttons.xml
android:state_pressed="true" />
android:state_focused="true" />
button_default.xml
android:radius="100dp"
/>
android:color="#FFFFFF"
/>
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
android:width="3dp"
android:color="#787878"
/>
在其它XML只更改颜色,所以我尽量避免将其发布。 这里是以编程方式更改Button的代码。 我拿起DB全部变更Button用ID保存并设置为Button的颜色。
//Get all materie inside database
List 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());
}
}
但我失去的半径和笔触属性。 有一些方法可以通过编程来设置它们吗? 接受其他建议!
2016-01-03
Dario