为什么我的android活动开始总是滚动到底部? [英] Why does my android activity always start scrolled to the bottom?

查看:98
本文介绍了为什么我的android活动开始总是滚动到底部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我开始这个活动,它总是开始触底反弹 - 滚动一路底部。我没有做什么奇怪的,在活动的OnCreate(或任何为此事),我希望改变滚动位置。我试图将焦点设置到最顶层的可聚焦控制和scrollto方法,但都没有工作。此外,没有我的其他活动有这个问题。这里的布局:

 <滚动型
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /滚动
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>
    <的LinearLayout
        机器人:方向=垂直
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:ID =@ + ID / edit_refresh_update_header
            机器人:TEXTSIZE =18sp/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=\ N
            机器人:TEXTSIZE =4SP/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=变量
            机器人:TEXTSIZE =18sp/>

        <微调
            机器人:ID =@ + ID / edit_refresh_variables_spinner
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=\ N
            机器人:TEXTSIZE =4SP/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=网络
            机器人:TEXTSIZE =18sp/>

        < RadioGroup中
            机器人:ID =@ + ID / WIDGET1
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:方向=垂直>

            <单选按钮
                机器人:文本=网络
                机器人:ID =@ + ID / edit_refresh_update_rb_web
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:的onClick =NetRadioButtonSelected
                机器人:检查=真/>

            <单选按钮
                机器人:文本=套接字服务器
                机器人:ID =@ + ID / edit_refresh_update_rb_socket_server
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:的onClick =NetRadioButtonSelected/>
        < / RadioGroup中>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=\ N
            机器人:TEXTSIZE =4SP/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=套接字服务器请求类型:
            机器人:TEXTSIZE =18sp/>

        <微调
            机器人:ID =@ + ID / edit_refresh_socket_server_req_types_spinner
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=\ N
            机器人:TEXTSIZE =4SP/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=插座服务器的身体:
            机器人:TEXTSIZE =18sp/>

        <的EditText
            机器人:layout_width =match_parent
            机器人:ID =@ + ID / edit_refresh_update_ss_body
            机器人:layout_height =WRAP_CONTENT
            机器人:启用=FALSE/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=\ N
            机器人:TEXTSIZE =4SP/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=URL:
            机器人:TEXTSIZE =18sp/>

        <的EditText
            机器人:layout_width =match_parent
            机器人:ID =@ + ID / edit_refresh_update_url
            机器人:layout_height =WRAP_CONTENT/>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=\ N
            机器人:TEXTSIZE =4SP/>

        <按钮
            机器人:文本=保存刷新更新
            机器人:ID =@ + ID / edit_refresh_save_btn
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_width =match_parent
            机器人:layout_marginLeft =20dip
            机器人:layout_marginRight =20dip
            机器人:layout_marginBottom =20dp
            机器人:layout_alignParentBottom =真
            机器人:的onClick =SaveRefreshUpdate>
        < /按钮>

        <的TextView
            机器人:layout_width =match_parent
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=\ N
            机器人:TEXTSIZE =4SP/>
    < / LinearLayout中>
< /滚动型>
 

解决方案

在Android的启动活动,一些控制需要采取重点。如果没有指定的控制采取集中,系统选择第一个符合条件的控制想要关注。 如果你在你的LinearLayout设置以下属性 - 安卓focusableInTouchMode =真正的的LinearLayout 将集中在开始和你的活动将不会滚动到的EditText 底部。

Whenever I start this activity, it always starts bottomed out--scrolled all the way to the bottom. I am not doing anything weird in the activity OnCreate (or anywhere for that matter) that I would expect to change the scroll position. I have tried setting focus to the topmost focusable control, and the scrollto method, but neither of them are working. Besides, none of my other activities has this problem. Here's the layout:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_refresh_update_header"
            android:textSize="18sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Variable:"
            android:textSize="18sp"/>

        <Spinner
            android:id="@+id/edit_refresh_variables_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Network:"
            android:textSize="18sp"/>

        <RadioGroup
            android:id="@+id/widget1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical">

            <RadioButton
                android:text="Web"
                android:id="@+id/edit_refresh_update_rb_web"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="NetRadioButtonSelected"
                android:checked="true"/>

            <RadioButton
                android:text="Socket Server"
                android:id="@+id/edit_refresh_update_rb_socket_server"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="NetRadioButtonSelected"/>
        </RadioGroup>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Socket server request type:"
            android:textSize="18sp"/>

        <Spinner
            android:id="@+id/edit_refresh_socket_server_req_types_spinner"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Socket server body:"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="match_parent"
            android:id="@+id/edit_refresh_update_ss_body"
            android:layout_height="wrap_content"
            android:enabled="false"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Url:"
            android:textSize="18sp"/>

        <EditText
            android:layout_width="match_parent"
            android:id="@+id/edit_refresh_update_url"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>

        <Button
            android:text="Save refresh update"
            android:id="@+id/edit_refresh_save_btn"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginBottom="20dp"
            android:layout_alignParentBottom="true"
            android:onClick="SaveRefreshUpdate">
        </Button>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="\n"
            android:textSize="4sp"/>
    </LinearLayout>
</ScrollView>

解决方案

When Android starts an activity, some control needs to take focus. When there's no designated control to take focus, the system chooses the first eligible control that wants focus. If you set the following property in your LinearLayout - android:focusableInTouchMode="true" your LinearLayout will be focused on start and your activity won't scroll to EditText in the bottom.

这篇关于为什么我的android活动开始总是滚动到底部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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