如何删除复选框旁边的多余空间?
首先,我是 Android 开发新手,一些看似简单的任务却让我沮丧不已。这是此处回答的问题的后续问题: 更改复选框大小在 Android 2.0 中
其要点是,由于某种原因,我的复选框旁边不断出现额外的空间(见下文)。
现在我尝试将宽度设置为 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).
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这看起来很可疑,就像“时间条目”标题位于标题行中自己的表格单元格中,并且其下方的复选框列与相同的宽度匹配。 (看到文本的右边缘如何与其下方的日期完美对齐吗?)由于复选框的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.
所以在另外两个回答者@adamp和@Phobos的输入之后。我偶然发现了对我有用的东西。
如何以编程方式设置(文本)视图属性?
通过添加以下内容:
我是能够按照我想要的方式调整该单元格。对我来说很奇怪的是,它不是在增长单元格的列,而是在增长复选框本身。这是因为在 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:
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.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)