setText,进入横向时重置和锁定

时间:2011-04-28 07:56:05

标签: android landscape android-1.5-cupcake settext

嘿,非常感谢您的回顾。 我真的不明白为什么会发生这种情况,尽管它可能与处理audioinput的后台线程有关。 一切正常,直到我倾斜手机,尚未放置风景画像或类似的东西。所以它应该自动伸展我的肖像布局。 但它只是拉伸它,它也会重置我的标签,并使我无法使用我的setText功能。我也注意到它暂停了我的后台线程。 以下是设置textViews的代码:

private void reactOnAudioMessage(Message msg) {
    TextView txtVwLastValue = (TextView) findViewById(R.id.txtVwLastValue);
    String result=null;
    DataFile newData=null;
    Bundle bndl=msg.getData();
    // ** newData getting created, with the correcy value etc this is not the problem.
    if(newData!=null){
        txtVwLastValue.setText("Last Value Received:" +newData.getValue());
        mDataList.add(newData);
        drawGraph(mImageView);
    } 
}

这是我的xml文件

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Record"
android:id="@+id/btnRecord">
</Button>
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Last Value Received:" android:id="@+id/txtVwLastValue"></TextView>
<TextView android:text="TextView" android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/txtVwType"></TextView>
<ImageView android:src="@drawable/icon" android:layout_width="fill_parent" android:id="@+id/imgViewGraph" android:layout_height="300px"></ImageView>
</LinearLayout>

3 个答案:

答案 0 :(得分:2)

So it should automatic just stretch my portrait layout.

正如您所看到的,这并非完全正确。未处理的方向更改的预期行为是重新创建Activity(这意味着遍历整个Activity生命周期,onDestroy(),onCreate()等等 - 显然是重置标签的原因)。这可以通过各种方式处理:

http://developer.android.com/guide/topics/resources/runtime-changes.html&amp; http://developer.android.com/resources/articles/faster-screen-orientation-change.html

我在我的应用程序中以两种相当简单的方式“处理”方向更改。通过在Manifest中设置screenOrientation标志,一个活动被锁定为纵向(因为在Landscape中查看它是没有意义的):

<activity android:name=".SettingsActivity" android:screenOrientation="portrait">

我使用的第二个Activity需要以两种方式查看,因为它包含一个在Landscape中查看时效果最佳的图表。这基本上意味着我确保我的Activity正常关闭(包括我使用的任何线程或处理程序)并重新绘制以横向方向绘制填充屏幕的图形。

您需要考虑如何处理方向更改。在您的应用程序中最有意义的是什么?关于通过Google进行方向变更的讨论,有很多很好的资源。

我想你在后台线程中遇到的麻烦是由于你在Activity中有的处理程序被销毁并重新制作。

答案 1 :(得分:2)

旋转设备后,它会有效地破坏并重新创建新方向的活动。如果您在活动过程中开始使用线程,那么这可以解释您所看到的一些行为。如果您希望线程在两个活动实例之间生存,那么您需要将其实现为服务而不是简单的线程。

证明这一点的方法是设置断点或在onCreate方法中添加一些日志记录,并且当应用程序启动并且首次显示活动时以及设备轮换时,您将看到此命中符。

答案 2 :(得分:0)

每次您倾斜手机时,即每次在纵向模式和横向模式之间切换时,活动都会重新开始,因此会执行onCreate()