在键盘上方显示一个视图

3
我正在开发一款应用程序,在其中我希望一个线性布局显示在键盘的顶部。当用户关闭键盘时,该线性布局也应该消失。我希望它像这样:
所需布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <RelativeLayout
            android:id="@+id/root"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

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

                <ImageView
                    android:id="@+id/iv_Img1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:scaleType="fitXY" />

                <ImageView
                    android:id="@+id/iv_Img2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:scaleType="fitXY" />
            </LinearLayout>
        </RelativeLayout>

        <ImageView
            android:id="@+id/iv_Edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:padding="5dp"
            android:scaleType="center"
            android:src="@drawable/cam" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/llEditOptions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_weight="0"
        android:visibility="gone" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" >

            <ImageView
                android:id="@+id/iv_background"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="50dp"
                android:layout_toLeftOf="@+id/iv_font"
                android:scaleType="center"
                android:src="@drawable/save_1" />

            <ImageView
                android:id="@+id/iv_font"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:scaleType="center"
                android:src="@drawable/cam" />

            <ImageView
                android:id="@+id/iv_size"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="50dp"
                android:layout_toRightOf="@+id/iv_font"
                android:scaleType="center"
                android:src="@drawable/share_1" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/llCamOptions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:layout_weight="0" >

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

            <ImageView
                android:id="@+id/iv_Save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginRight="50dp"
                android:layout_toLeftOf="@+id/iv_Camera"
                android:scaleType="center"
                android:src="@drawable/save_1" />

            <ImageView
                android:id="@+id/iv_Camera"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:scaleType="center"
                android:src="@drawable/cam" />

            <ImageView
                android:id="@+id/iv_Share"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="50dp"
                android:layout_toRightOf="@+id/iv_Camera"
                android:scaleType="center"
                android:src="@drawable/share_1" />
        </RelativeLayout>
    </LinearLayout>

</LinearLayout>

这条消息来自于 iOS 手机,但我们能在 Android 上实现这个功能吗?

1
你有试过什么吗?你的代码在哪里? - Westranger
我没有找到如何做的任何信息。请问你能指导我吗? - Anuj
请在问题中添加您的布局代码。 - Ando Masahashi
我已经更新了我的帖子,请看一下。 - Anuj
您的问题解决了吗?还需要进一步的帮助吗?请不要忘记给最有帮助的答案点赞/接受答案。 - Xaver Kapeller
这个问题已经在这篇帖子中得到了回答。 - Westranger
1个回答

9

当键盘显示时,整个布局会被压缩,因为可用的空间较小。您需要将 LinearLayout 对齐到屏幕的底部,这样它就会显示在键盘的正上方。例如,您可以使用以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="60dp"
        android:clipToPadding="false">

        <RelativeLayout
            android:id="@+id/rlContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            ... // The content of your layout goes here            

        </RelativeLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/llFooter"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true">

        // This LinearLayout will be aligned to the 
        // bottom of the screen and displayed above your keyboard

    </LinearLayout>   

</RelativeLayout> 
ScrollView只是为了使您的布局在键盘打开并且没有足够的空间来一次性显示所有内容时可以滚动。底部的LinearLayout将独立于屏幕底部定位,并覆盖其他任何内容。 ScrollView还具有与页脚View相等的填充,并将clipToPadding设置为false。这意味着ScrollView可以使用填充来显示内容。否则,您的布局底部的一部分将被隐藏在底部的LinearLayout后面。当滚动时,您可以使LinearLayout稍微透明以获得良好的效果。

但是当键盘被隐藏时,我不想显示那个布局。 - Anuj
@user3917131 所以只需要隐藏它。问题具体是什么? - Xaver Kapeller
你的意思是什么?你使用了上面的确切布局吗?如果你是以编程方式添加了“View”,你确定你已经正确设置了所有相关属性吗? - Xaver Kapeller
我刚刚测试了我的解决方案,它完美地运行了。 - Xaver Kapeller
1
让页脚只在键盘显示时可见几乎是不可能的。您可以通过将其初始可见性设置为View.GONE,然后在EditTexts上设置一个OnFocusChangedListener,如果其中一个具有焦点,则可以将页脚的可见性设置为View.VISIBLE来接近实现此目标。但这永远不会完美地工作。无法监听键盘打开或关闭事件。我认为您能够可靠地实现所需的操作的唯一方法是编写自定义键盘。 - Xaver Kapeller
显示剩余2条评论

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