【问题标题】:How do I keep ScrollView from scrolling down on orientation change?如何防止 ScrollView 在方向更改时向下滚动?
【发布时间】:2015-09-14 03:32:02
【问题描述】:

我有一个LinearLayout,它非常适合改变方向。然后我将 LinearLayout 包装在 ScrollView 中,现在我遇到了问题。垂直滚动工作正常,但是当方向更改为横向模式时,视图会向下滚动一点,所以现在布局的顶部被切断了。 ScrollView 似乎正在响应位于视图顶部下方的 EditText 行上的 requestFocus() 代码。但是 View 足够大,可以显示 View 的顶部和 EditText 行。如何在方向更改后强制视图不向下滚动,以便它可以显示布局的顶部和 EditText 行?

部分布局文件:

?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".CardViewActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" >
</include>

<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"  >

<LinearLayout
    android:id="@+id/LinearLayout1"
    style="@style/scrollbar_shape_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_corner"
    android:layout_marginLeft="6dp"
    android:layout_marginStart="6dp"
    android:layout_marginRight="6dp"
    android:layout_marginEnd="6dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:useDefaultMargins="false"
    android:orientation="vertical"  >
    ...

部分活动文件:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardviewinput);

    cListenerEditText = (ListenerEditText) findViewById(R.id.CEditText);
    cListenerEditText.requestFocus();

【问题讨论】:

    标签: android android-layout android-scrollview


    【解决方案1】:

    在AndroidManifest.xml的activity标签中加入如下属性 android:windowSoftInputMode="adjustPan"

    【讨论】:

    • 我目前将其设置为“stateVisible|adjustResize”,因为我使用软键盘进行用户输入,显示在主视图下方。想法?
    • 尝试将其更改为“stateVisible|adjustPan”
    • 好的,我会试试的。 “adjustPan”会做什么而不是“adjustResize”?
    • 好的。并且更改为 adjustPan 不会“不太理想”的原因是因为 ScrollView 允许用户向下滚动以查看 View 的其余部分?
    • 哦,是的,刚刚意识到这个问题。我想在这种情况下,唯一的解决方案是用户按键盘上的 NEXT 键进入下一个 editText,或者按 DONE 关闭键盘。
    猜你喜欢
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2019-07-05
    相关资源
    最近更新 更多