【问题标题】:Chat window ScrollView expanded downwards instead of upwards聊天窗口 ScrollView 向下扩展而不是向上扩展
【发布时间】:2013-09-22 12:56:26
【问题描述】:

我想实现一个聊天窗口。也就是说,用户在窗口底部书写,新文本向上滚动现有文本。为此,我创建了一个 ScrollView,里面有一个 LinearLayout。新的 EditText 视图被添加到 LinearLayout 从底部开始,根据重力布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="bottom"
    android:layout_gravity="bottom"
    android:fillViewport="true"
    tools:context=".Chat">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"/>

</ScrollView>

这很好用,但是当我想查看屏幕上方的 “隐藏文本” 并向上滚动时,我已经在滚动的末尾并且隐藏的文本有消失了。相反,ScrollView 允许我向下滚动,并且新的空白区域已添加到视图底部。

只是为了测试相反的方向,我换行了:

android:gravity="bottom"
android:layout_gravity="bottom"

android:gravity="top"
android:layout_gravity="top"

对于 ScrollViewLinearLayout 并添加索引为 0 的新 EditText 视图,而不是末尾的默认值:

linearLayout.addView(editText, 0);

而且聊天窗口在相反的方向上从上到下都能很好地工作。它可以向下滚动到输入的第一行。什么都没有丢失。

所以我的问题是,第一种方法必须添加什么才能使滚动保持 LinearLayout 的内容并且不丢失任何文本?

【问题讨论】:

    标签: android scroll android-linearlayout android-scrollview


    【解决方案1】:

    好的,我终于通过查看 Android 资源发现了发生了什么。

    android:layout_gravity="top" 必须在 LinearLayout 中设置。

    解释:

    子定位在 ScrollView 的 onLayout() 处计算,正好在父级的 onLayout() 中,在类 FrameView 中。在那里,如果child(即LinearLayout)的layout_gravity为bottom,因为它比ScrollView大,所以topborder在屏幕之外,具有负定位。因此,列表在顶部被剪切。 LinearLayout 的顶部边框位置必须为 0,这是通过将 layout_gravity 设置为顶部来实现的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-07
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2015-09-03
      相关资源
      最近更新 更多