当活动从后台转到前台时,按钮的文本布局突然从上到下变化
我注意到一个非常奇怪的行为。我的布局的一部分生成这些按钮:
这是相应的布局:
<TableLayout
android:id="@+id/mainscreenButtonbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="bottom"
android:stretchColumns="*" >
<TextView
android:id="@+id/statusBar"
style="@style/StatusBarStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/connecting" />
<TableRow
style="@style/ButtonStyle">
<Button
android:id="@+id/btnReport"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blitzerMelden"
android:textSize="18sp" />
<Button
android:id="@+id/btnConfigurations"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/bundeslaender"
android:textSize="18sp" />
</TableRow>
</TableLayout>
这是使用的样式:
<style name="ButtonStyle">
<item name="android:background">#212121</item>
</style>
<style name="StatusBarStyle">
<item name="android:textSize">11sp</item>
<item name="android:background">#212121</item>
<item name="android:padding">2sp</item>
</style>
现在,当且仅当此 Activity 进入后台(通过单击 HOME)并将恢复时,才会发生奇怪的情况。突然,按钮文本从上到下变化:
我不明白为什么会发生这种情况。它不依赖于textView。当我省略 TextView 时,行为是相同的。当我使用 LinearLayout (没有这个 TextView)而不是这个 TableLayout 时,行为是正确的并且文本不会改变:
<LinearLayout
android:id="@+id/mainscreenButtonbar"
style="@style/ButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:stretchColumns="*" >
<Button
android:id="@+id/btnReport"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blitzerMelden"
android:textSize="18sp" />
<Button
android:id="@+id/btnConfigurations"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/bundeslaender"
android:textSize="18sp" />
</LinearLayout>
所以这个错误的行为无论如何都与 TableLayout 相关,但是如何呢?有什么想法吗?
更新
虽然它对按钮没有做任何特殊的事情,但我添加了 onResume 的 Activity 代码(我说过,它仅在 Activity 将恢复时发生,但前提是我按 HOME 退出它如果我在不同的 Activity 之间切换,或者甚至从新启动应用程序,则不会出现该问题。)
@Override
protected void onResume() {
super.onResume();
setConnectionText(getString(R.string.connecting));
messageTable.removeAllViewsInLayout();
Log.d(TAG, "> executing onResume");
try {
Log.d(TAG, "> reading config data");
stateStates = dataProvider.getConfigData();
}
catch (PolizeiwarnungException e) {
Toast toast = Toast.makeText(this, "Fehler: " + e.getMessage(), 4000);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
try {
String receivedJsonString = httpConnector.receiveData();
HashMap<String, ArrayList<String>> messages = jsonParser.jsonStringToMessageMap(receivedJsonString);
for (Map.Entry<String, Boolean> state : stateStates.entrySet()) {
buildMessageTable(state.getKey(), state.getValue(), messages);
}
}
catch (PolizeiwarnungException e) {
setConnectionText(getString(R.string.dataDownloadFailed));
addTextToTable(getConnectionText());
}
TableRow tableRow = new TableRow(this);
InvisibleButton invisibleButton = new InvisibleButton(this);
tableRow.addView(invisibleButton);
messageTable.addView(tableRow);
}
(buildMessageTable 将接收到的数据添加到上面的 TableLayout,因此这不会影响此问题)
I keep track of a very strange behavior. One part of my layout produces these buttons:
This is the corresponding layout:
<TableLayout
android:id="@+id/mainscreenButtonbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="bottom"
android:stretchColumns="*" >
<TextView
android:id="@+id/statusBar"
style="@style/StatusBarStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/connecting" />
<TableRow
style="@style/ButtonStyle">
<Button
android:id="@+id/btnReport"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blitzerMelden"
android:textSize="18sp" />
<Button
android:id="@+id/btnConfigurations"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/bundeslaender"
android:textSize="18sp" />
</TableRow>
</TableLayout>
Here are the used styles:
<style name="ButtonStyle">
<item name="android:background">#212121</item>
</style>
<style name="StatusBarStyle">
<item name="android:textSize">11sp</item>
<item name="android:background">#212121</item>
<item name="android:padding">2sp</item>
</style>
Now, something strange happens if and only if this Activity goes into background (by clicking HOME) and will be resumed. Suddenly the button text ranges from top to bottom:
I don't understand why this happens. It doesn't depend on the textView. When I omit the TextView, the behavior is the same. When I use a LinearLayout (without this TextView) instead of this TableLayout, the behavior is correct and the text doesn't change:
<LinearLayout
android:id="@+id/mainscreenButtonbar"
style="@style/ButtonStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:stretchColumns="*" >
<Button
android:id="@+id/btnReport"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/blitzerMelden"
android:textSize="18sp" />
<Button
android:id="@+id/btnConfigurations"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/bundeslaender"
android:textSize="18sp" />
</LinearLayout>
So this wrong behavior relates anyhow to the TableLayout, but how? Any ideas?
UPDATE
Althought it doesn't do anything special to the buttons I add the Activity code of onResume (I said, it only happens when the Activity will be resumed, but only if I have exit it with pressing HOME before. If I switch between the different Activities or if I even start the app from new, the problem doesn't appear.)
@Override
protected void onResume() {
super.onResume();
setConnectionText(getString(R.string.connecting));
messageTable.removeAllViewsInLayout();
Log.d(TAG, "> executing onResume");
try {
Log.d(TAG, "> reading config data");
stateStates = dataProvider.getConfigData();
}
catch (PolizeiwarnungException e) {
Toast toast = Toast.makeText(this, "Fehler: " + e.getMessage(), 4000);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
}
try {
String receivedJsonString = httpConnector.receiveData();
HashMap<String, ArrayList<String>> messages = jsonParser.jsonStringToMessageMap(receivedJsonString);
for (Map.Entry<String, Boolean> state : stateStates.entrySet()) {
buildMessageTable(state.getKey(), state.getValue(), messages);
}
}
catch (PolizeiwarnungException e) {
setConnectionText(getString(R.string.dataDownloadFailed));
addTextToTable(getConnectionText());
}
TableRow tableRow = new TableRow(this);
InvisibleButton invisibleButton = new InvisibleButton(this);
tableRow.addView(invisibleButton);
messageTable.addView(tableRow);
}
(buildMessageTable adds the received data to a TableLayout above, so this doesn't affect this problem)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不得不再试一次。虽然我无法完全复制您的错误,但我能够在按下主页按钮后返回时强制按钮不显示任何文本(空白按钮)。我通过在每个按钮上的 onResume 末尾添加代码来解决此问题。我认为这也适用于您的情况,因为它可能会强制重新计算按钮中文本的宽度。也许值得一试?
Had to give it one more try. While I could not exactly duplicate your error, I was able to force the buttons to not show any text (blank buttons) when returning after Home button was hit. I fixed this by adding code at end of onResume to setText on each button. I'm thinking this could work in your situation also because it might force recalculation of width of text in button. Might be worth a try?