android:调用 Button.setText() 会导致 Button 发生意外的重新定位
我遇到这个问题,每当我调用 Button.setText(String) 时,
。我没有给出任何指示 Button
的 TableLayout
就会重新定位所有 Button
对象View
应该被移动......而且我真的不希望它这样做。如果有人知道如何防止这些Views
移动(如果可能的话,不会有太多麻烦),请告诉我。
我正在使用一些非正统的方法来获取屏幕设置。这是代码:
static_grid_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:stretchColumns="*"
android:gravity="center">
<TableRow android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button android:id="@+id/btn1"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn2"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn3"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn4"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn5"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn6"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn7"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn8"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn9"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn10"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn11"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn12"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn13"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn14"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn15"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn16"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn17"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn18"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn19"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn20"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn21"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
</TableLayout>
MainActivity.java:特别感兴趣的部分将在这里:
query = "SELECT * FROM "+DbSchema.ProductSchema.TABLE_NAME+
" ORDER BY "+DbSchema.ProductSchema.COLUMN_PRIORITY+" ASC";
updateList(listQuery(query));
fillPage(mPage);
setContentView(mPage);
在 onResume()
覆盖中的代码底部。这是图像中的不良行为。当我调用 setText() 时,按钮仅获得延迟的 y 偏移量。如果我调用 setText("") [空字符串] 按钮的位置不会改变。什么给了?
I'm having this problem where a TableLayout
of Button
s is relocating all Button
objects whenever i call Button.setText(String)
. i've given no indication that the View
should be moved... and i really don't want it to. if anyone knows how to keep these Views
from moving (without a whole lot of hassle if possible), please let me know.
i'm using some unorthodox methods for getting the screen setup. here's the code:
static_grid_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:stretchColumns="*"
android:gravity="center">
<TableRow android:layout_height="fill_parent"
android:layout_width="fill_parent">
<Button android:id="@+id/btn1"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn2"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn3"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn4"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn5"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn6"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn7"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn8"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn9"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn10"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn11"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn12"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn13"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn14"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn15"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn16"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn17"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn18"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
<TableRow>
<Button android:id="@+id/btn19"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn20"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
<Button android:id="@+id/btn21"
android:layout_width="100dip"
android:layout_height="80dip"
android:gravity="center"
android:ellipsize="marquee"/>
</TableRow>
</TableLayout>
MainActivity.java: the part of particular interest will be here:
query = "SELECT * FROM "+DbSchema.ProductSchema.TABLE_NAME+
" ORDER BY "+DbSchema.ProductSchema.COLUMN_PRIORITY+" ASC";
updateList(listQuery(query));
fillPage(mPage);
setContentView(mPage);
toward the bottom of the code in the onResume()
override. here is the undesired behavior in an image. the Button
s ONLY get that retarded y offset when i call setText(). if i call setText("") [empty String] the location of the button does not change. what gives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明这是 TableLayouts 的一个已知问题。早在 2010 年初,此问题已被多次报告。您可以在此处找到错误报告:
http://code.google.com/p/android/issues/detail?id=6428
如果您有任何需要添加的内容,或者您有相关问题,请发布错误报告。
修复:为了解决这个问题,我最终在填写网格时用换行符填充了空白按钮。这样一切就都抵消了。漂亮、整洁、均匀。
turns out this is a known issue with TableLayouts. this was reported several times, looking back as far as early 2010. you can find the bug report here:
http://code.google.com/p/android/issues/detail?id=6428
if you have anything to add, or you have a related issue, please post a bug report.
FIX: to fix the issue, i ended up filling the blank buttons with newlines as i filled the grid out. that way everything was offset together. nice, neat, even.
尝试更改您写入按钮的文本,如果 setText("") 不更改布局,我怀疑按钮对于您要插入的文本量来说太小。
另外,在第一个表行中,删除 android:layout_height="fill_parent"。
该行将尝试使该行填充整个表格,将其更改为wrap_content,如果文本不太适合按钮,则布局不应移动。
Try changing the text you are writing into the button, I suspect the buttons are too small for the amount of text you are inserting if setText("") doesn't change the layout.
Also, in your first tablerow, remove android:layout_height="fill_parent".
That line will try to make that row fill the entire table, change it to wrap_content and if the text isn't too much to fit in the button, the layout should not shift.