当软键盘弹出时,如何固定一些组件?

7
我的应用程序是一个聊天应用程序,其界面如下:
应用程序的标题
列表视图
聊天消息、输入框和发送按钮
但问题是,当我点击编辑文本时,软键盘会弹出并将所有内容推上去,但我需要标题留在屏幕上。我在这里附上了简化的问题图像和UI代码(点击此处)。(我不能在此帖子中发布多个链接或图像,因为我是Stack Overflow的新手。)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView android:text="Header contents" android:layout_width="fill_parent"
        android:textSize="20sp" android:gravity="center_horizontal"
        android:layout_height="wrap_content" />
    <ListView android:id="@android:id/list" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_weight="1" />

    <EditText android:text="" android:layout_gravity="bottom"
        android:id="@+id/EditText01" android:layout_width="fill_parent"
        android:layout_height="wrap_content"></EditText>
</LinearLayout>

Android默认的消息应用有此要求。

有人有什么想法吗?

3个回答

4
这个问题很老,但是无论如何 - 修复它的一种方法是将标题下面的所有内容放入ScrollView中。
示例:
<!-- Both elements below are children of a parent RelativeLayout -->

<RelativeLayout
    android:id="@+id/custom_action_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:isScrollContainer="false" >

    <!-- Header goes here. -->
    <!-- This part remains fixed even with the keypad popping up. -->

</RelativeLayout>

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/custom_action_bar"
    android:isScrollContainer="true" >

    <!-- Rest of the UI -->
    <!-- This part scrolls to accommodate the keyboard -->

 </ScrollView>

这是唯一可行的解决方案,如果您有多个视图,并且需要将其中几个保持静态位置,让其他视图可以调整大小或平移。在清单文件的activity/application标签中添加“android:windowSoftInputMode="adjustPan"”。 - Bijay Koirala

1

这里描述的解决方案就是解决方案。你的全屏主题导致Kamil Los无法复制你的问题。

你需要调整活动的windowSoftInputMode标签,如这里所述。

尝试在你的应用程序清单中的有问题的活动中添加标签"android:windowSoftInputMode="adjustResize""。


-1

这很奇怪,因为我创建了一个新项目,粘贴了你的布局,在我的情况下标题栏没有消失(无论是在2.2模拟器还是1.6上)。


好的,我明白问题了,但还没有解决。在我的应用程序中,我所做的是使用全屏。这就是为什么键盘会推动一切的原因,请查看此项目“http://www.freefilehosting.net/chatuilayout”。 - Prasad De Zoysa
请尝试此处描述的解决方案 - https://dev59.com/_W855IYBdhLWcg3wq2XL - Kamil Łoś
1
这并没有解决我的问题。问题是如果我处于全屏模式,然后键盘出现并将所有内容推上去。 - Prasad De Zoysa

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