滚动视图不起作用。

5
我知道应该很容易!!!我看过教程,在网上搜索了大约2小时并尝试了15种不同的方法(可能更多),但仍然无法使其正常工作。在我的应用程序中,当我聚焦/选择EditText并弹出键盘时,我无法向下滚动以输入其他文本。在AndroidManifest.xml中,将activity设置为android:windowSoftInputMode="adjustPan"。下面是layout>main.xml的结构。
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:padding="20dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <TextView
            android:id="@+id/txtProduct1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Produkt 1"
            android:textColor="#FFFFFF"
            android:textSize="28dp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtInfo1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:gravity="right"
            android:text="cena / hmot: 0 eur"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#EEEEEE"
            android:width="140dp" />
    </LinearLayout>

    <EditText
        android:id="@+id/txtPrice1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="10dp"
        android:ems="10"
        android:hint="Cena"
        android:inputType="numberDecimal" >
    </EditText>

    <EditText
        android:id="@+id/txtWeight1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:ems="10"
        android:hint="Hmotnosť"
        android:inputType="numberDecimal" />

    <ImageView
        android:id="@+id/imgResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/question" />

    <EditText
        android:id="@+id/txtPrice2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Cena"
        android:inputType="numberDecimal" />

    <EditText
        android:id="@+id/txtWeight2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:ems="10"
        android:hint="Hmotnosť"
        android:inputType="numberDecimal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/txtInfo2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginRight="5dp"
            android:text="cena / hmot: 0 eur"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="#EEEEEE"
            android:width="140dp" />

        <TextView
            android:id="@+id/txtProduct2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Produkt 2"
            android:textColor="#FFFFFF"
            android:textSize="28dp"
            android:textStyle="bold" />
    </LinearLayout>

    <Button
        android:id="@+id/btnOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:height="80dp"
        android:text="OK"
        android:textColor="#264865"
        android:textSize="24dp"
        android:textStyle="bold"
        android:width="160dp" />
 </LinearLayout>

</ScrollView>

你实际上没有在问一个问题.. 但是TextView的id为 txtInfo1txtInfo2,它们的android:layout_height="fill_parent",我建议先将其改为wrap_content - Blundell
1
我假设你有那个闭合的</ScrollView>标签,对吧?另外,请详细描述什么没有起作用?是编译时出现错误吗?还是它不像你希望的那样出现? - Amokrane Chentir
@AmokraneChentir 是的,他只是格式错误了。 - Blundell
1
“还是无法使其正常工作”这样的陈述毫无意义。如果您需要帮助,必须提供完整精确的症状描述。 - CommonsWare
这是什么15种不同的方法? - Shahzad Imam
显示剩余2条评论
3个回答

4

有几件事情需要注意:

  1. 确保 ScrollView 只有一个子元素,

  2. 如已提到的,请确保您有一个结束标签

  3. 以下代码适用于我...

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent" 
  android:layout_height="fill_parent"
  android:fillViewport="true" >

  <LinearLayout
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >

          <!--I added more views here! -->

  </LinearLayout>
</ScrollView>

希望这能帮到您。

------------------------------------------------------------------------------------------

根据评论更新... 我在三星Galaxy S上复制并粘贴您的代码时的屏幕截图。正常工作!

您在Galaxy S上的布局



  1. 它只有一个子元素,就像您的示例;
  2. 结束标签已经存在;
- vstruhar
我已经在键盘可见时进行了检查,但我无法向下滚动以查看其他输入内容。当EditText获得焦点时,我想要更改LinearLayout的高度(当前高度+键盘高度)。 - vstruhar
我将您的布局代码复制并粘贴到一个新项目中(只是用ic_launcher替换了您的drawables)。当我在我的三星Galaxy S上运行时,视图确实向上滚动以显示位于键盘下方的EditText,所以我不知道为什么它对您无效... - Mel
@vstruhar,你找到为什么会发生这种情况了吗?可能是当你隐藏状态栏时出现的错误吗? - android developer
不,我没有。我只是停止使用Theme.NoTitleBar.Fullscreen。 - vstruhar
显示剩余5条评论

2
您最后没有写上 </ScrollView>,只需添加即可完成。

请在评论中给出这样的答案。 - Shahzad Imam
@ShahzadImam:好的,我明白你的意思。 - Bhavin

1
在我的情况下,ScrollView 上有一个 OnTouchListener,在 onTouch 方法中返回 true,导致滚动无法正常工作。将其改为返回 false 可以解决这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接