开发者

How to remove this extra space next to a checkbox?

开发者 https://www.devze.com 2023-02-15 05:00 出处:网络
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

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).

How to remove this extra space next to a checkbox?

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");
            c开发者_JAVA技巧ustomerBox.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!


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.


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!


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)

0

精彩评论

暂无评论...
验证码 换一张
取 消