Android中的按钮列表 [英] List of button in Android

查看:644
本文介绍了Android中的按钮列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个Activity中创建一个ImageButtons列表,每行三个按钮.如果我使用XML做到这一点(考虑到有超过100个按钮),那么eclipse会抱怨说有很多视图.

I want to make a list of ImageButtons in an Activity with three buttons in each row. If I make this with XML (considering there are over 100 buttons) eclipse complains that there are to many views.

是否有更好的方法可以做到这一点?谢谢!

Is there a better way to do this? Thanks!

推荐答案

我认为您应该动态地制作按钮..

I think you should make buttons dinamically.. like this

Button[] btnWord = new Button[num];
 LinearLayout linear;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.dynamicview);
  test();
 }
 private void test() {  
  linear = (LinearLayout) findViewById(R.id.linear);
  for (int i = 0; i < btnWord.length; i++) {
   btnWord[i] = new Button(this);
   btnWord[i].setHeight(50);
   btnWord[i].setWidth(50);
   btnWord[i].setTag(i);
   btnWord[i].setOnClickListener(btnClicked);
   linear.addView(btnWord[i]);
  }
 }
 OnClickListener btnClicked = new OnClickListener() {
  @Override
  public void onClick(View v) {
   Object tag = v.getTag();
   Toast.makeText(getApplicationContext(), "clicked button", Toast.LENGTH_SHORT).show();
  }
 };

您可以更改按钮阵列的数量

you can change the number of button array

Button[] btnword = new Button[num];

这篇关于Android中的按钮列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆