【问题标题】:Add an array of buttons to a GridView in an Android application将一组按钮添加到 Android 应用程序中的 GridView
【发布时间】:2010-10-20 23:37:47
【问题描述】:

我有一个应用程序,它有 5-15 个按钮,具体取决于后端可用的内容。如何定义正确的 GridView 布局文件以包含一组按钮,每个按钮都有不同的文本和其他属性?每个按钮本质上都会将一个项目添加到购物车,因此除了它添加到购物车的项目之外,onClick 代码将是相同的。

如何定义一个数组,以便添加可变数量的按钮,但仍通过唯一 ID 引用每个按钮?我见过arrays.xml 的示例,但他们创建了一个预先设置的字符串数组。我需要一种方法来创建一个对象,而不是在布局或数组 xml 文件中定义文本。

更新 - 添加了有关添加到 GridView 的信息

我想将此添加到 GridView,因此调用 [addView 方法](http://developer.android.com/reference/android/widget/AdapterView.html#addView(android.view.View,%20int) 会导致 UnsupportedOperationException。我可以执行以下操作:

ImageButton b2 = new ImageButton(getApplicationContext());
b2.setBackgroundResource(R.drawable.img_3);
android.widget.LinearLayout container = (android.widget.LinearLayout) findViewById(R.id.lay);
container.addView(b2);

但这并没有像我想要的那样在网格中布置按钮。这可以在 GridView 中完成吗?

【问题讨论】:

    标签: android arrays button dynamic android-gridview


    【解决方案1】:

    在下面的代码中,您应该将for 的上限更改为一个变量。

    public class MainActivity
            extends Activity
            implements View.OnClickListener {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            TableLayout layout = new TableLayout (this);
            layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
    
            layout.setPadding(1,1,1,1);
    
            for (int f=0; f<=13; f++) {
                TableRow tr = new TableRow(this);
                for (int c=0; c<=9; c++) {
                    Button b = new Button (this);
                    b.setText(""+f+c);
                    b.setTextSize(10.0f);
                    b.setTextColor(Color.rgb( 100, 200, 200));
                    b.setOnClickListener(this);
                    tr.addView(b, 30,30);
                } // for
                layout.addView(tr);
            } // for
    
            super.setContentView(layout);
        } // ()
    
        public void onClick(View view) {
            ((Button) view).setText("*");
            ((Button) view).setEnabled(false);
        }
    } // class
    

    【讨论】:

      【解决方案2】:

      这是一个不错的示例:

      https://developer.android.com/guide/topics/ui/layout/gridview.html

      您应该只在 getView 适配器方法中创建按钮而不是图像视图。

      【讨论】:

        【解决方案3】:

        如果您正在使用 GridViewListView (etc),并且正在生成视图以通过适配器 getView(pos, convertView, viewGroup),你可能会遇到困惑(我曾经做过一次)。

        如果您决定重新使用 convertView 参数,则必须重置其中的所有内容。这是框架传递给您的旧视图,以节省膨胀布局的成本。它几乎从不与之前在布局中的位置相关联。

        class GridAdapter extends BaseAdapter // assigned to your GridView
        {
            public View getView(int position, View convertView, ViewGroup arg2) {
                View view;
                if (convertView==null)
                {
                    view = getLayoutInflater().inflate(R.layout.gd_grid_cell, null);
                }
                else
                {
                     // reusing this view saves inflate cost
                     // but you really have to restore everything within it to the state you want
                    view = convertView;
                }
        
        
                return view;
            }
            //  other methods omitted (e.g. getCount, etc) 
        }
        

        我认为这代表了 Android 的一些东西,一开始这个概念有点难以理解,直到你意识到其中有一个重要的优化可用(必须对小型移动设备上的 CPU 很好)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-06
          • 2016-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多