Java 如何以编程方式绘制ImageButtons的5x5矩阵?

Java 如何以编程方式绘制ImageButtons的5x5矩阵?,java,android,Java,Android,我需要ImageButtons具有相同的高度和宽度,彼此之间没有如下图所示的空格,并且在矩阵的开头和结尾不得有此类空格 多谢各位! 另外,我在这里使用的代码是` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:

我需要ImageButtons具有相同的高度和宽度,彼此之间没有如下图所示的空格,并且在矩阵的开头和结尾不得有此类空格

多谢各位! 另外,我在这里使用的代码是`

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="*" 
        android:layout_marginTop="-4.5dp"
        android:paddingBottom="-5dp"
        android:paddingLeft="-5dp"
        android:paddingRight="-5dp"
        android:paddingTop="-5dp" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
             >

            <ImageButton
                android:layout_height="fill_parent"
                android:id="@+id/ImageButton_11"
                android:layout_width="0dp"
                android:layout_weight="1"
                />

            <ImageButton
                android:id="@+id/ImageButton_12"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_marginLeft="-8.5dp"
                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/ImageButton_13"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:layout_marginLeft="-8.5dp"
                />

            <ImageButton
                android:id="@+id/ImageButton_14"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:layout_marginLeft="-8.5dp"
                 />

            <ImageButton
                android:id="@+id/ImageButton_15"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:layout_marginLeft="-8.5dp"
                 />

        </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="-10dp" >

            <ImageButton
                android:id="@+id/ImageButton_21"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"

                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/ImageButton_22"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                />

            <ImageButton
                android:id="@+id/ImageButton_23"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

            <ImageButton
                android:id="@+id/ImageButton_24"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                />

            <ImageButton
                android:id="@+id/ImageButton_25"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="-10dp" >

            <ImageButton
                android:id="@+id/ImageButton_31"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"

                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/ImageButton_32"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                />

            <ImageButton
                android:id="@+id/ImageButton_33"
                android:layout_width="0dp"
               android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                  android:layout_marginLeft="-8.5dp"
                  />

            <ImageButton
                android:id="@+id/ImageButton_34"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

            <ImageButton
                android:id="@+id/ImageButton_35"
                android:layout_width="0dp"
               android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

        </TableRow>

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="-10dp"
                     >

            <ImageButton
                android:id="@+id/ImageButton_41"
                android:layout_width="0dp"
               android:scaleType="centerCrop"
                android:layout_height="fill_parent"

                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/ImageButton_42"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                />

            <ImageButton
                android:id="@+id/ImageButton_43"
                android:layout_width="0dp"
               android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                />

            <ImageButton
                android:id="@+id/ImageButton_44"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

            <ImageButton
                android:id="@+id/ImageButton_45"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

        </TableRow>

                <TableRow
                    android:id="@+id/tableRow5"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="-10dp" >

            <ImageButton
                    android:id="@+id/ImageButton_51"
                    android:layout_width="0dp"
                    android:scaleType="centerCrop"
                    android:layout_height="fill_parent"

                    android:layout_weight="1" />

            <ImageButton
                android:id="@+id/ImageButton_52"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

            <ImageButton
                android:id="@+id/ImageButton_53"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

            <ImageButton
                android:id="@+id/ImageButton_54"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

            <ImageButton
                android:id="@+id/ImageButton_55"
                android:layout_width="0dp"
                android:scaleType="centerCrop"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                 android:layout_marginLeft="-8.5dp"
                 />

        </TableRow>
    </TableLayout>

    <ImageButton 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/refresh_ImageButton"

        />

</LinearLayout>

现在,我希望以编程方式完成此操作。

您的xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="container"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

</LinearLayout>

我知道它看起来不像你的,但它应该让你知道怎么做

我的回答稍有延迟(与上面的答案非常相似,因此我很抱歉,但我正在寻找另一种方法来删除按钮的边距:)

编辑2
您可以通过更好的方式进行覆盖,使其更为通用,但对于您的情况,这将起作用

请将您的代码添加到问题中。您必须删除任何边距或填充。@HosseinGerami添加了一个边距或填充,但它只是一个XML表示形式。如果不推荐使用“填充父项”,请改用“匹配父项”。这和你的答案是一样的:)我对解决这个问题失去了希望:最后一件事,我如何设置单元格的高度与宽度相等?谢谢,我不是安卓系统专家,但我能找到两种可能的解决方案:一是使用方形图像,二是使用正确的比例类型,你应该能够做到这一点。2-扩展Layout类并覆盖onResize方法(我将把它添加到我的答案中)
LinearLayout container = (LinearLayout) activity.findViewById(R.id.container); 
TableLayout table = new TableLayout(activity);
LayoutParams matchWrap = new LinearLayout.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams zeroMatchOne = new TableRow.LayoutParams(
                0, LayoutParams.MATCH_PARENT, 1f);
for (int i=0; i<5; i++) {
  TableRow row = new TableRow(activity);
  row.setLayoutParams(matchWrap);
  for(int j=0; j<5; j++) {
    ImageButton image = new ImageButton(activity);
    image.setLayoutParams(zeroMatchOne);
    row.addView(image);
  }
  table.addView(row);
}
layoutParams.setMargins(int left, int top, int right, int bottom);
view.setPadding(int left, int top, int right, int bottom);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout mainContainer = (LinearLayout)findViewById(R.id.main_container);

    int cols = 5;
    int rows = 5;


    //Params used for cells
    TableRow.LayoutParams params = new TableRow.LayoutParams(
            0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f
    );
    int pixelsToDp = convertToPixelsDp(-4.5f);
    params.setMargins(pixelsToDp, pixelsToDp, pixelsToDp, pixelsToDp);

    //Params used for rows
    TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
    );
    rowParams.setMargins(0, 0, 0, 0);

    //Params used for table
    TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
    );
    tableParams.setMargins(0, 0, 0, 0);

    //Table generation
    TableLayout table = new TableLayout(this);
    table.setLayoutParams(tableParams);
    table.setShowDividers(TableLayout.SHOW_DIVIDER_NONE);
    table.setStretchAllColumns(true);

    for(int i = 0; i < rows; i++){

        TableRow row = new TableRow(this);

        row.setShowDividers(TableRow.SHOW_DIVIDER_NONE);
        row.setLayoutParams(rowParams);

        for(int j = 0; j < cols; j++){

            ImageButton btn = new ImageButton(this);

            btn.setPadding(0, 0, 0, 0);
            btn.setLayoutParams(params);
            btn.setScaleType(ImageView.ScaleType.CENTER_CROP);

            //The button's background has the unneeded margin
            //so if you set correct background drawable I think
            //you might achieve what you want API lvl 16 above
            //btn.setBackground(null);

            //API Level 17 and above
            //If you want id's for some reason you could store them in
            //some kind of Dictionary or you could store the UI elements
            //to have access to them

            //int id = btn.generateViewId();
            //btn.setId(id);

            row.addView(btn);
        }

        table.addView(row);
    }

    mainContainer.addView(table);

}

public int convertToPixelsDp(float pixels){
    int px = (int)TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            pixels,
            this.getResources().getDisplayMetrics());
    return px;
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
    <item 
        android:state_pressed="true"
        android:drawable="@drawable/down_button_lesser_opacity_red_pressed"
        />
<!-- Normal -->
    <item
        android:drawable="@drawable/down_button_lesser_opacity_red_normal"/>
</selector>
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:width="0dp" android:color="@color/transparent" />
    <solid android:color="#55FF0000"/>
    <corners 
        android:topLeftRadius="0dp" android:topRightRadius="0dp" 
        android:bottomLeftRadius="6dp" android:bottomRightRadius="6dp" 
        />
    <padding 
        android:left="6dp" android:top="0dp"
        android:right="6dp" android:bottom="6dp" 
        />
    </shape> 
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:width="0dp" android:color="@color/transparent" />
    <solid android:color="@color/feature_normal_item"/>
    <corners 
        android:topLeftRadius="0dp" android:topRightRadius="0dp" 
        android:bottomLeftRadius="6dp" android:bottomRightRadius="6dp" 
        />
    <padding 
        android:left="6dp" android:top="0dp"
        android:right="6dp" android:bottom="6dp" 
        />
</shape> 
FrameLayout btn = new FrameLayout(this);
btn.setPadding(0, 0, 0, 0);
btn.setLayoutParams(new TableRow.LayoutParams(
    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
));

TextView text = new TextView(this);
text.setText("Button: " + i + j);

btn.addView(text);
btn.setBackgroundResource(R.drawable.down_button_lesser_opacity_selector);
btn.setClickable(true);
class MyFrameLayout extends FrameLayout
{
    MyFrameLayout(Context context){
        super(context);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh){
        Log.v("measure", "width:" + w + " height:" + h);
        if(w != h) {
            this.setLayoutParams(new TableRow.LayoutParams(
                    w, w
            ));

        }
    }
}