将一组按钮添加到 Android 应用程序中的 GridView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/775188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add an array of buttons to a GridView in an Android application
提问by Tai Squared
I have an application that will have 5-15 buttons depending on what is available from a backend. How do I define the proper GridView layout files to include an array of buttons that will each have different text and other attributes? Each button will essentially add an item to a cart, so the onClick code will be the same except for the item it adds to the cart.
我有一个应用程序,它有 5-15 个按钮,具体取决于后端可用的按钮。如何定义正确的 GridView 布局文件以包含一组按钮,每个按钮将具有不同的文本和其他属性?每个按钮基本上都会将一个项目添加到购物车,因此除了添加到购物车的项目之外,onClick 代码将相同。
How can I define an array so I can add a variable number of buttons, but still reference each of them by a unique ID? I've seen examples of the arrays.xml, but they have created an array of strings that are pre-set. I need a way to create an object and not have the text defined in the layout or arrays xml file.
如何定义数组以便添加可变数量的按钮,但仍然通过唯一 ID 引用每个按钮?我已经看到了arrays.xml 的例子,但他们已经创建了一个预先设置的字符串数组。我需要一种方法来创建一个对象,而不是在布局或数组 xml 文件中定义文本。
Update - Added info about adding to a GridView
更新 - 添加了有关添加到 GridView 的信息
I want to add this to a GridView, so calling the [addView method](http://developer.android.com/reference/android/widget/AdapterView.html#addView(android.view.View,%20int)results in an UnsupportedOperationException. I can do the following:
我想将此添加到 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);
but that doesn't layout the buttons in a grid like I would like. Can this be done in a GridView?
但这并没有像我想要的那样在网格中布置按钮。这可以在 GridView 中完成吗?
回答by cibercitizen1
In the following code, you should change the upper limits of the for
to a variable.
在以下代码中,您应该将 的上限更改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
回答by Fedor
Here's a nice sample for you:
这是一个很好的示例:
https://developer.android.com/guide/topics/ui/layout/gridview.html
https://developer.android.com/guide/topics/ui/layout/gridview.html
You should just create buttons instead of imageviews in getView adapter method.
您应该只在 getView 适配器方法中创建按钮而不是图像视图。
回答by alienjazzcat
If you are using a GridView, or a ListView (etc), and are producing Views to populate them via the adapter getView(pos, convertView, viewGroup), you might encounter confusion (i did once).
如果您正在使用GridView或ListView(等),并且正在生成视图以通过适配器getView(pos, convertView, viewGroup)填充它们,您可能会遇到混淆(我做过一次)。
If you decide to re-use the convertView parameter, you must reset everythinginside of it. It is an old view being passed to you by the framework, in order to save the cost of inflating the layout. It is almost never associated with the positionit was in the layout before.
如果您决定重新使用 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)
}
I think this represents one of those Android things where the concept is a little difficult to grasp at first, until you realize there's a significant optimization available within it (have to be nice to CPU on a little mobile device)
我认为这代表了其中一个概念在开始时有点难以理解的 Android 事物之一,直到您意识到其中有一个重要的优化可用(必须对小型移动设备上的 CPU 友好)