【问题标题】:Scrollview does not shrink when soft keyboard is shown显示软键盘时滚动视图不会缩小
【发布时间】:2014-10-08 20:47:28
【问题描述】:

我有一个简单的聊天活动,顶部有一个消息输入框,然后是带有消息列表的滚动视图。打开软键盘时,我希望滚动视图缩小,以便键盘不会覆盖最后一条消息。这是我的 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="wrap_content"
android:padding="8dp" >

<TextView
    android:id="@+id/chat_with"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="Chat with Gordon" />

<RelativeLayout
    android:id="@+id/msg_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/chat_with"
    android:layout_marginTop="10dp">

    <EditText
        android:id="@+id/chat_message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Type your message" />

    <ImageButton
        android:id="@+id/chat_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="#00000000"
        android:src="@+android:drawable/ic_menu_send" />
</RelativeLayout>



<ScrollView
    android:id="@+id/chat_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/msg_container"
    android:layout_marginTop="15dp"
    android:padding="10dp" 
    android:isScrollContainer="true"
    >

    <RelativeLayout
        android:id="@+id/chat_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        >

        <TextView
            android:id="@+id/msg1"          
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hi, how are you?" 
            android:layout_alignParentRight="true"/>

        <TextView
            android:id="@+id/msg2"  
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hey! All right" 
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/msg1"/>



    </RelativeLayout>

</ScrollView>

</RelativeLayout>

我只是手动插入了两条消息,但实际应用中会有很多消息。我已经尝试使用 adjustPan|adjustResize 解决方案,但它不起作用,当我触发键盘时,布局没有改变,最后的消息被键盘覆盖。我怎样才能避免这种情况? 感谢您的帮助!

【问题讨论】:

  • 一个你应该使用列表视图而不是滚动视图,两个只是滚动到列表的末尾
  • 滚动到列表末尾不是问题,我可以通过编程方式完成。问题是键盘覆盖了消息。为什么我不应该使用滚动视图?
  • 阅读列表视图的作用并找出为什么要使用它developer.android.com/guide/topics/ui/layout/listview.html。键盘打开时无法显示最后一条消息,消息可能真的很长
  • 我知道列表视图是什么,但我仍然不明白为什么它会缩小而滚动视图不会。但我会阅读更多内容。
  • 我从来没有说过我会说没有办法显示整个消息,并且动态显示许多消息时滚动视图在这里不合适

标签: android android-layout android-scrollview


【解决方案1】:

我通过使用清单解决了这个问题。在我使用的活动中:

        android:windowSoftInputMode="stateHidden|adjustResize"

这样,键盘开始隐藏,然后当用户单击 EditText 时,键盘会显示出来,屏幕的其余部分会调整大小。

查看 windowSoftInputMode 以了解键盘行为的其他设置(例如,仅覆盖屏幕而不是调整大小)。

编辑: 要在键盘出现后将滚动视图滚动到底部,我需要稍微延迟一下,因为有时滚动会先发生,然后键盘出现,滚动视图不再位于底部。由于其他原因,我最终没有保留它,但是为了延迟滚动,可以这样做:

    // now scroll to the bottom
    ScrollView sv = (ScrollView) findViewById(R.id.myScrollView);
    sv.post(new Runnable() {
        @Override

        public void run() {

            ScrollView sv = (ScrollView) findViewById(R.id.myScrollView);
            sv.scrollTo(0, sv.getBottom());

        }
    });

【讨论】:

  • 酷,谢谢!为了让它完全工作,我应该让它在软键盘打开时滚动到最后,但这是另一个问题。
  • @splinter123 - 我添加了一些代码来展示如何以编程方式滚动滚动视图。
  • 好的,但该代码何时执行?我想它应该放在某种监听器中,一旦显示软键盘就会收到通知。我做了一些研究,但仍然没有找到一个干净的解决方案。
  • 我可能会在 GlobalLayoutListener 中添加这部分代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-03
  • 2016-01-08
  • 1970-01-01
相关资源
最近更新 更多