如何删除复选框旁边的多余空间?

发布于 2024-10-21 04:05:03 字数 2614 浏览 1 评论 0原文

首先,我是 Android 开发新手,一些看似简单的任务却让我沮丧不已。这是此处回答的问题的后续问题: 更改复选框大小在 Android 2.0 中

其要点是,由于某种原因,我的复选框旁边不断出现额外的空间(见下文)。

Space Next to checkboxes

现在我尝试将宽度设置为 1,但它不会比显示的更短。我尝试过将宽度设置得更大,可以使尺寸更大,但不能更短。

请看一下下面的代码,然后告诉我您看到了什么。

TableLayout table = (TableLayout) findViewById(R.id.timeEntriesTable);


        for (int i = 0; i < timeEntries.getLength(); i++)
        {
            Element element = (Element) timeEntries.item(i);
            TableRow tr = new TableRow(this);
            tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            CheckBox box = new CheckBox(this);
            box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
            box.setPadding(0,0,0,0);
            box.setWidth(1);
            box.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    CheckBox box = (CheckBox) v;
                    if (box.isChecked())
                    {
                        box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_on_background));
                    }
                    else
                    {
                        box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
                    }

                }
            });

            TextView dateBox = new TextView(this);

            String dateText = element.getAttribute("date");
            dateBox.setText(dateText);
            dateBox.setTextColor(Color.BLACK);

            TextView customerBox = new TextView(this);

            String customerName = element.getAttribute("customer");
            customerBox.setText(customerName);
            customerBox.setTextColor(Color.BLACK);

            TextView hoursBox = new TextView(this);

            String hours = element.getAttribute("hours");
            hoursBox.setText(hours);
            hoursBox.setTextColor(Color.BLACK);

            TextView test = new TextView(this);
            test.setTextColor(Color.RED);
            test.setText("Test");

            tr.addView(box);                
            tr.addView(dateBox);
            tr.addView(customerBox);
            tr.addView(hoursBox);



            table.addView(tr, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }

先感谢您!

First off I am new to android development and some seemingly easy tasks are frustrating me to no end. This is a follow on question to what was answered here: Changing the checkbox size in Android 2.0

The gist of it is that for some reason there is this extra space that keeps showing up next to my checkboxes (See Below).

Space Next to checkboxes

Now I have tried to set the width to 1 and it doesn't get any shorter than what is shown. I have tried to set the width bigger and I can make the size bigger, but no shorter.

Please take a look at the code below and let me know what you see.

TableLayout table = (TableLayout) findViewById(R.id.timeEntriesTable);


        for (int i = 0; i < timeEntries.getLength(); i++)
        {
            Element element = (Element) timeEntries.item(i);
            TableRow tr = new TableRow(this);
            tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            CheckBox box = new CheckBox(this);
            box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
            box.setPadding(0,0,0,0);
            box.setWidth(1);
            box.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    CheckBox box = (CheckBox) v;
                    if (box.isChecked())
                    {
                        box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_on_background));
                    }
                    else
                    {
                        box.setButtonDrawable(getResources().getDrawable(R.drawable.checkbox_off_background));
                    }

                }
            });

            TextView dateBox = new TextView(this);

            String dateText = element.getAttribute("date");
            dateBox.setText(dateText);
            dateBox.setTextColor(Color.BLACK);

            TextView customerBox = new TextView(this);

            String customerName = element.getAttribute("customer");
            customerBox.setText(customerName);
            customerBox.setTextColor(Color.BLACK);

            TextView hoursBox = new TextView(this);

            String hours = element.getAttribute("hours");
            hoursBox.setText(hours);
            hoursBox.setTextColor(Color.BLACK);

            TextView test = new TextView(this);
            test.setTextColor(Color.RED);
            test.setText("Test");

            tr.addView(box);                
            tr.addView(dateBox);
            tr.addView(customerBox);
            tr.addView(hoursBox);



            table.addView(tr, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
        }

Thank you in advance!

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

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

发布评论

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

评论(3

我要还你自由 2024-10-28 04:05:03

这看起来很可疑,就像“时间条目”标题位于标题行中自己的表格单元格中,并且其下方的复选框列与相同的宽度匹配。 (看到文本的右边缘如何与其下方的日期完美对齐吗?)由于复选框的layout_gravity 可能默认为TOP|LEFT,因此它位于单元格的左上角。

尝试更改标题行或复选框的布局重力。

That looks suspiciously like the "Time Entries" header is in its own table cell in a header row and the column of checkboxes below it is matching the same width. (See how the right edge of the text lines up perfectly with the date below it?) Since the checkbox's layout_gravity is likely defaulting to TOP|LEFT, it's being positioned in the upper left of the cell.

Try altering either the header row or the layout_gravity of the checkboxes.

少女七分熟 2024-10-28 04:05:03

所以在另外两个回答者@adamp和@Phobos的输入之后。我偶然发现了对我有用的东西。

如何以编程方式设置(文本)视图属性?

通过添加以下内容:

TableRow.LayoutParams lp = new TableRow.LayoutParams();
            lp.width = 15;
            lp.setMargins(3, 5, 0, 0);
            lp.height = 15;
tr.addView(box, lp);

我是能够按照我想要的方式调整该单元格。对我来说很奇怪的是,它不是在增长单元格的列,而是在增长复选框本身。这是因为在 Android 中没有实际的单元格,它显然使用视图及其大小来生成表格。

我没有遇到过任何教程或任何可以让您以编程方式设置 LayoutParams 的内容。感谢您帮助解决此问题!

So after the input of the two other answerers @adamp and @Phobos. I stumbled across what worked for me.

How to set (Text)View attributes programmatically?

By adding this:

TableRow.LayoutParams lp = new TableRow.LayoutParams();
            lp.width = 15;
            lp.setMargins(3, 5, 0, 0);
            lp.height = 15;
tr.addView(box, lp);

I was able to tweak that cell exactly how I wanted it. It was very strange to me that it wasn't growing the column of the cell it was growing the checkbox itself. This is because in Android there are no actual cells, it uses the views and their sizes to generate the table apparently.

I have came across no tutorials or anything that has you set the LayoutParams programmatically that way. Thank you for your help troubleshooting this!

初见终念 2024-10-28 04:05:03

表格布局自然希望所有单元格具有相同的大小。如果您希望一个或多个具有不同的大小,则需要额外的参数。

要缩小单元格中的所有额外空间,请将其添加到表格对象

table.setShrinkAllColumns(true);

或者只是缩小复选框为

table.setColumnShrinkable(int columnIndex, boolean isShrinkable)的第一列

Table Layouts naturally want to make all the cells the same size. If you want one, or more, to be different sizes then that takes additional parameters.

To shrink all the extra space in your cells add this to your table object

table.setShrinkAllColumns(true);

Or to just shrink the first column where your checkbox is

table.setColumnShrinkable(int columnIndex, boolean isShrinkable)

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