【发布时间】:2016-02-22 19:32:04
【问题描述】:
我正在动态生成一些 ToggleButtons,但我没有设置上边距以避免按钮在它们之间连接。
我的代码是下一个,我做错了什么?
private void generateToggleButton(){
RelativeLayout.LayoutParams lvg = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, R.dimen.tv_et_standart_small);
lvg.height = R.dimen.tv_et_standart_small;
lvg.width = RelativeLayout.LayoutParams.MATCH_PARENT;
lvg.setMargins(0, R.dimen.dp8, 0 ,0);
for(int e = 0; e < new_exp_Especies.size(); e++){
final int position = e;
ToggleButton tb = new ToggleButton(this);
tb.setBackgroundDrawable(getResources().getDrawable(R.drawable.customshape_orange_red));
tb.setText(new_exp_Especies.get(position).EspecieDesc);
tb.setTextOff(new_exp_Especies.get(position).EspecieDesc);
tb.setTextOn(new_exp_Especies.get(position).EspecieDesc);
tb.setTextSize(12);
tb.setGravity(Gravity.CENTER);
tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.setBackgroundDrawable(getResources().getDrawable(R.drawable.customshape_green));
new_exp_Especies.get(position).setValue(1);
} else {
buttonView.setBackgroundDrawable(getResources().getDrawable(R.drawable.customshape_orange_red));
new_exp_Especies.get(position).setValue(0);
}
}
});
tb.setLayoutParams(lvg);
ll_tbt_especie.addView(tb);
}
}
【问题讨论】:
-
你需要获得价值......阅读文档:
R.dimen.tv_et_standart_small,R.dimen.dp8不是你想的那样......你需要使用来自Resources类的类正确方法
标签: android margin togglebutton