Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式创建8x32图像按钮阵列_Android_Android Layout - Fatal编程技术网

Android 以编程方式创建8x32图像按钮阵列

Android 以编程方式创建8x32图像按钮阵列,android,android-layout,Android,Android Layout,我正在创建一个通过蓝牙控制微控制器的应用程序。它将是一个8x32 Led矩阵。 其想法是创建一个8行32列的Imagebuttons数组,每个元素在电路板上的真实LED上表示 在一些文档之后,我决定使用TableLayout并用ImageButtons填充它。但无法让它工作 这就是我到目前为止所做的: public class DrawerMode extends Activity implements View.OnTouchListener{ private static int numbe

我正在创建一个通过蓝牙控制微控制器的应用程序。它将是一个8x32 Led矩阵。 其想法是创建一个8行32列的Imagebuttons数组,每个元素在电路板上的真实LED上表示

在一些文档之后,我决定使用TableLayout并用ImageButtons填充它。但无法让它工作

这就是我到目前为止所做的:

public class DrawerMode extends Activity implements View.OnTouchListener{
private static int numberOfColumns = 32;
private static int numberOfRows = 8;
private TableLayout tableLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    //Assign content
    setContentView(R.layout.activity_draw_mod);
    tableLayout = (TableLayout) findViewById(R.id.drawer_table);
    fillTable(numberOfColumns,numberOfRows, tableLayout);


}

private void fillTable(final int numberOfColumns,final int numberOfRows, TableLayout tableLayout) {
    //removing child views
    tableLayout.removeAllViews();
    TableLayout.LayoutParams params = new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);

    //initiate rows
    TableRow[] tableRow = new TableRow[numberOfRows];
    for(int iter_R = 0; iter_R < numberOfRows; iter_R++){
        //fill rows with buttons
        tableRow[iter_R] = new TableRow(this);
        ImageButton[] imageButton = new ImageButton[numberOfColumns];
        for(int iter_C = 0; iter_C < numberOfColumns; iter_C++){
            imageButton[iter_C]  = new ImageButton(this);
            imageButton[iter_C].setBackgroundColor(Color.WHITE);
            imageButton[iter_C].setLayoutParams(params);

            //[iter_C]
            imageButton[iter_C].setOnTouchListener(this);
            imageButton[iter_C].setId(iter_C);
            imageButton[iter_C].setTag(iter_C);
            //add an instance of a button
            tableRow[iter_R].addView(imageButton[iter_C]);
        }
        //add instance of row
        tableLayout.addView(tableRow[iter_R]);
    }
}



@Override
public boolean onTouch(View v, MotionEvent event) {
    int selected_element = (Integer) v.getId();
    Toast.makeText(getApplicationContext(), "It's"+ selected_element, Toast.LENGTH_LONG).show();
    return false;
}
}
以下是要访问的xml文件的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@android:color/holo_green_dark">
<TextView
    android:id="@+id/draw_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/draw_mode_name"
    android:layout_centerHorizontal="true"
    android:textSize="24sp"
    android:textColor="@android:color/white"
    />
<TableLayout
    android:id="@+id/drawer_table"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/draw_title"
    >

    </TableLayout>

对我来说,最好的方法是什么?

您可以创建8个id为的线性布局,然后为每个布局添加32个按钮

我不能接受任何答案,因为他们不能用TableLayout解决这个问题。最后,经过更详细的搜索,我找到了一个解决此问题的优秀教程:

检查动态按钮和图像教程。这个解决方案要归功于Brian

我遇到了一个问题,但出于我的目的解决了它,可以在这里查看:


谢谢帕特里克和福克斯各自的回答。如果有人能发布另一个关于TableLayout的详细解决方案,除了这个,我会接受这个答案。

你是什么意思,但不能让它工作?怎么了?什么也没显示出来将xml文件添加到,可能我遗漏了一些内容。我不想重复那么多次代码,这就是为什么我尝试以编程方式解决此问题。在xml中使用垂直线性布局,然后垂直循环以创建水平线性布局,怎么样,然后水平循环以在每个水平布局内创建按钮?这可能会起作用,但我希望有一个直接而清晰的解决方案,没有解决方法:。但我会记住你的想法。