滚动型不滚动和方向改变应用程序重新启动 [英] ScrollView not scrolling and application restart on orientation change

查看:222
本文介绍了滚动型不滚动和方向改变应用程序重新启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个Android应用程序,它与蓝牙模块连接。当创建应用程序,以执行该模块的螺纹来连接。当我旋转手机,应用程序重新启动和连接线再次运行。为了解决这个问题,我添加

I am building an Android application which connects with a Bluetooth module. When the application is created, a thread to connect to the module is executed. When i rotate the phone, the application restarts and the connection thread is run again. To get around this, I added

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

到Android清单文件。现在,应用程序没有得到重新启动,但我在活动中使用的滚动型不滚动。任何想法,以重新启动获得滚动视图滚动和prevent应用?

to the Android manifest file. Now the application doesn't get restarted, but a ScrollView I used in the activity does not scroll. Any idea to get the scrollview scrollable and prevent the application from restarting?

下面是我的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" />

推荐答案

活动被摧毁和重建的屏幕旋转,你可能想要这个,因为它正确地重新调整你的所有UI元素。

Activities get destroyed and recreated on screen rotation, and you probably want this as it resizes all your UI elements properly.

创建一个单独的单例类蓝牙连接/线程,使其成为独立的活动,或使其相关活动成员静态的。

Create your Bluetooth connection/thread in a separate singleton class so it becomes independent of the activity, or make its relevant activity members static.

但是,请务必正确地摧毁它,所以它不漏/持续超过合法应用程序生命周期。

However, be sure to destroy it properly so it doesn't leak/persist beyond the legitimate application lifecycle.

这篇关于滚动型不滚动和方向改变应用程序重新启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆