如何设置按钮背景图像以动态地将内容包裹在表格布局中

发布于 2024-10-09 17:55:21 字数 1413 浏览 0 评论 0原文

我在下面动态实现表格布局代码。

private void showCowsTblField() {
    for (int row = 0; row < numberOfRowsInField - 1; row++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        for (int column = 0; column < numberOfColumnsInField - 1; column++) {
            LayoutParams layoutParams = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            if (column == 0) {
                textSno[row][column].setLayoutParams(layoutParams);
                layoutParams.setMargins(0, 0, 0, 0);
                tableRow.addView(textSno[row][column]);
            } else {
                blocks[row][column].setLayoutParams(layoutParams);
                layoutParams.setMargins(1, 1, 1, 1);
                tableRow.addView(blocks[row][column]);
            }
            tableRow.setBackgroundResource(R.drawable.bar_1);
        }
        tblCows.addView(tableRow, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    }
}

这里的 block 是一个扩展 Button 类的类, 在此类中,我将背景图像设置为

this.setBackgroundResource(R.drawable.edit_button);

这里的问题是按钮背景图像大小(高度)正在更改以适合表格行高度。这看起来不太好。

我如何将按钮背景图像设置为表行中的实际大小(wrapcontent)。

请告诉我.... 谢谢.........

Im implementing tablelayout dynamically code below.

private void showCowsTblField() {
    for (int row = 0; row < numberOfRowsInField - 1; row++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        for (int column = 0; column < numberOfColumnsInField - 1; column++) {
            LayoutParams layoutParams = new LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            if (column == 0) {
                textSno[row][column].setLayoutParams(layoutParams);
                layoutParams.setMargins(0, 0, 0, 0);
                tableRow.addView(textSno[row][column]);
            } else {
                blocks[row][column].setLayoutParams(layoutParams);
                layoutParams.setMargins(1, 1, 1, 1);
                tableRow.addView(blocks[row][column]);
            }
            tableRow.setBackgroundResource(R.drawable.bar_1);
        }
        tblCows.addView(tableRow, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    }
}

here block is a class extends Button class,
in this class iam setting background image as

this.setBackgroundResource(R.drawable.edit_button);

Here problem is the button background image size(height) is changing to fit to tablerow height. which is not looking good.

How i can set button background image to actual size (wrapcontent) in table row.

Please let me know....
Thanks.........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无戏配角 2024-10-16 17:55:21

看看是否有帮助 - http://developer.android.com/reference/ android/widget/ImageView.ScaleType.html

仅当使用 http://developer.android.com/reference/android/widget/ImageButton.html

您的代码必须如下所示:

class MyButton extends ImageButton{...}


this.setScaleType(scaleType);

See if that helps - http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

You can set it only if you use http://developer.android.com/reference/android/widget/ImageButton.html

Your code must look something like this:

class MyButton extends ImageButton{...}


this.setScaleType(scaleType);
还给你自由 2024-10-16 17:55:21

我在课堂上尝试了以下代码;它扩展了按钮类

this.setMaxHeight(26);
this.setMaxWidth(54); 

解决了问题。:)

I tried the following code in my class; which extends button class

this.setMaxHeight(26);
this.setMaxWidth(54); 

solved issue.:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文