ScrollView 不滚动并且应用程序在方向更改时重新启动

ScrollView not scrolling and application restart on orientation change

我正在构建一个与蓝牙模块连接的 Android 应用程序。创建应用程序时,将执行连接到模块的线程。当我旋转 phone 时,应用程序重新启动并且连接线程再次变为 运行。为了解决这个问题,我添加了

android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustResize|stateHidden"

到 Android 清单文件。现在应用程序没有重新启动,但是我在 activity 中使用的 ScrollView 没有滚动。有什么想法可以使滚动视图可滚动并防止应用程序重新启动吗?

这是我的 XML 文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_above="@+id/imageView">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/imageView"
        android:background="#bf000000"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/txtReceive1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <TableLayout
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff3f3f2"
            android:shrinkColumns="*"
            android:stretchColumns="*">

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"

                    android:padding="8dip"
                    android:text="CH" />

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="kWh" />

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="Load" />

                <TextView
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="Date" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/t11"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="1" />

                <TextView
                    android:id="@+id/t12"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t13"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t14"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/t21"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="2" />

                <TextView
                    android:id="@+id/t22"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t23"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t24"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView

                    android:id="@+id/t31"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="3" />

                <TextView

                    android:id="@+id/t32"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:id="@+id/t34"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView

                    android:id="@+id/t41"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip"
                    android:text="4" />

                <TextView
                    android:id="@+id/t42"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:id="@+id/t43"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView

                    android:id="@+id/t44"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/t51"
                    android:layout_width="wrap_content"

                    android:gravity="center"
                    android:padding="8dip"
                    android:text="5" />

                <TextView
                    android:id="@+id/t52"

                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t53"

                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />

                <TextView
                    android:id="@+id/t54"
                    android:layout_width="wrap_content"
                    android:gravity="center"
                    android:padding="8dip" />
            </TableRow>
        </TableLayout>

        <TextView
            android:id="@+id/txtReceive"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/table"
            android:background="#ff8c00"
            android:padding="6dip" />

        <LinearLayout
            android:id="@+id/lin2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:paddingTop="5dp"
            android:visibility="visible"
            android:weightSum="2">

            <LinearLayout
                android:id="@+id/rel"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:orientation="vertical"
                android:visibility="visible"
                android:weightSum="4">


                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="@drawable/btn_01_small"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton1"
                        android:layout_centerHorizontal="true"
                        android:text="Channel"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView9"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton1"
                        android:layout_centerHorizontal="true"
                        android:text="0"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton2"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_02"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton2"
                        android:layout_centerHorizontal="true"
                        android:text="kWh"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView10"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton2"
                        android:layout_centerHorizontal="true"
                        android:text="000.0"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton3"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_03"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton3"
                        android:layout_centerHorizontal="true"
                        android:text="Load"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView11"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton3"
                        android:layout_centerHorizontal="true"
                        android:text="000"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton4"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_04_small" />

                    <TextView
                        android:id="@+id/textView8"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton4"
                        android:layout_centerHorizontal="true"
                        android:text="Days"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView12"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton4"
                        android:layout_centerHorizontal="true"
                        android:text="000"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />

                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/rel2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentRight="true"
                android:layout_below="@+id/txtReceive"
                android:layout_weight="1"
                android:orientation="vertical"
                android:visibility="visible"
                android:weightSum="4">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton12"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:background="@drawable/btn_01_small"
                        android:textColor="#ffa5ff45" />

                    <TextView
                        android:id="@+id/textView32"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton12"
                        android:layout_centerHorizontal="true"
                        android:text="Auto ON"
                        android:textSize="22sp" />

                    <TextView
                        android:id="@+id/textView92"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton12"
                        android:layout_centerHorizontal="true"
                        android:text="00:00"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />

                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton22"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:background="@drawable/btn_04_small"
                        android:textColor="#ffa5ff45" />


                    <TextView
                        android:id="@+id/textView62"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignTop="@+id/imageButton22"
                        android:layout_centerHorizontal="true"
                        android:text="Auto OFF"
                        android:textSize="22sp" />


                    <TextView
                        android:id="@+id/textView102"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/imageButton22"
                        android:layout_centerHorizontal="true"
                        android:text="00:00"
                        android:textColor="#ffa5ff45"
                        android:textSize="22sp" />


                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton31"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="-14dp"
                        android:layout_marginTop="7dp"
                        android:background="@drawable/send_btn_01"
                        android:text="SET TIME"
                        android:textSize="22sp" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/imageButton32"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="-7dp"
                        android:background="@drawable/send_btn_01"
                        android:padding="0dp"
                        android:scaleType="matrix"
                        android:text="RECHARGE"
                        android:textSize="22sp" />
                </RelativeLayout>

            </LinearLayout>
        </LinearLayout>


    </LinearLayout>
</ScrollView>

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:adjustViewBounds="true"
    android:background="#bf000000"
    android:src="@drawable/base_irya_left1" />

应用程序不会在屏幕旋转时重新启动,activity 会。

Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

你能告诉我们 ScrollView 的 XML 声明吗?

Activity 在屏幕旋转时被销毁并重新创建,您可能希望这样做,因为它会正确调整所有 UI 元素的大小。

在单独的单例 class 中创建您的蓝牙 connection/thread,使其独立于 activity,或使其相关 activity 成员静态。

但是,请务必妥善销毁它,以免它 leak/persist 超出合法的应用程序生命周期。