开发者

Android: Dynamically add custom checkboxes to a tableLayout

What should I do to use custom checkboxes if I add my checkboxes dynamically in my code? (On the java code 开发者_如何转开发not on the XML files.) I'm following this tutorial, but using it I can't achieve my goal.

For example, I have a tableLayout and I want to add a checkbox for each new row I have.

Thank you all.


If you want to add custom checkboxes try this:

StateListDrawable stateList = new StateListDrawable();
int statePressed = android.R.attr.state_pressed;
int stateChecked = android.R.attr.state_checked;
stateList.addState(new int[] {-stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_3)));
stateList.addState(new int[] {stateChecked}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_1)));
stateList.addState(new int[] {statePressed}, new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.check_2)));
final CheckBox box = new CheckBox(this);
box.setButtonDrawable(stateList);


you can do it like this

TableLayout tl=new TableLayout(this);  
 TableRow tr=new TableRow(this);  
 CheckBox chk=new CheckBox(this);  
 chk.setText("Hello");  
 tr.addView(chk);         
 tl.addView(tr);  
 setContentView(tl); 

hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜