简体   繁体   English

在Android上使用多个按钮进行布局的最佳方法

[英]Best way to make a layout with many buttons on Android

First I have build a simple android application which had only five optios to select. 首先,我建立了一个简单的android应用程序,只有五个选项可供选择。 For this purpose I used five Buttons on main Activity. 为此,我在主活动上使用了五个按钮。 Now I have more than twenty buttons in a ScrollView to select. 现在,在ScrollView中有二十多个按钮可供选择。 What is the best way to represent this kind of application (using buttons in a ScrollView? using TabHost? or with some other widget?) 代表这种应用程序的最佳方法是什么(使用ScrollView中的按钮?使用TabHost?还是使用其他小部件?)

The app look like this now: 该应用现在看起来像这样:

在此处输入图片说明

Grid View or List View or Recycler View Grid ViewList ViewRecycler View

the Adapter automatically will add buttons with the names you want, something that I did for my upcoming app. 适配器会自动添加带有所需名称的按钮,这是我为即将发布的应用所做的。 I made a java class called data which has 'data` for my app. 我制作了一个名为data的Java类,该类的应用程序具有“ data ”。 it has an array of images for my GridView. 它为我的GridView提供了一系列图像。

SO: 所以:

  • Make a class called data 制作一个称为数据的类
  • Add a public final static String[] myArray array of your names, or data 添加名称或数据的public final static String[] myArray数组
  • Now, whenever you want to access them, use data.myArray 现在,每当您要访问它们时,请使用data.myArray
  • If you want to access one item ,use data.myArray[itemIndex] 如果要访问一项,请使用data.myArray [itemIndex]
  • Don't forget, indexes are zero based, not 1 别忘了,索引从零开始,而不是1
  • Put your button inside a viewHolder class 将您的按钮放在viewHolder类中
  • find the id of the button in the getView if convertView is null & set the holder as a tag 如果convertViewnull ,则在getView找到按钮的ID,并将该holder设置为标记

NOTE : after finding the ID of the button, just leave it don't do anything or edit the text, continue reading please. 注意:找到按钮的ID后,请不要进行任何操作或编辑文本,请继续阅读。

  • Use that array with your custom adapter as 将该阵列与您的自定义适配器一起使用

gridView.setAdapter(new myCustomAdapter(parameter1, parameter2,data.myArray);

use this , I just made it yesterday, added array of buttons feature now. 使用这个 ,我昨天才做的,现在添加了按钮数组功能。 You can just learn it or use it or commit changes. 您可以学习或使用它,也可以提交更改。

NOTE : 注意 :

  • You can make an array of listeners just like any primitive data type, View.OnClickListener[] and name it, initialize it. 您可以像任何原始数据类型View.OnClickListener[]一样View.OnClickListener[]一个侦听器数组,并将其命名,初始化。

Use grid view . 使用grid view it will be easy to show multiple buttons on screen using grid view. 使用网格视图很容易在屏幕上显示多个按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM