在键盘上方显示视图 [英] Display a view on top of keyboard

查看:100
本文介绍了在键盘上方显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个我希望在键盘顶部显示线性布局的应用程序.当用户关闭键盘时,线性布局也应该消失. 我想要这样的东西

I am making a application in which i want a linear layout to be displayed on top of the keyboard.When the user closes the keyboard that linear layout should also be gone. I want it something like this

布局我想要的东西

<?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中实现此功能吗?

This is from ios phone but can we implement this functionality in android???

推荐答案

显示键盘时,会压缩整个布局,因为可用空间较少.您所要做的就是将LinearLayout对准屏幕底部,它将直接显示在键盘上方.例如,您可以使用如下布局:

When the keyboard is shown the whole layout is squashed because there is less space available. All you have to do is align the LinearLayout to the bottom of the screen and it will be displayed directly above the keyboard. You could for example use a layout like this:

<?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将独立放置在屏幕底部,并且与其他任何地方重叠.

The ScrollView is just there to make your layout scrollable when the keyboard opens and there isn't enough room anymore to display all of it at once. The LinearLayout at the bottom will be positioned independently at the bottom of the screen and overlap anything else.

ScrollView的填充也等于页脚View的大小,并且clipToPadding设置为false.这意味着ScrollView可以使用填充来显示内容.您需要这样做,否则布局的底部将被隐藏在底部的LinearLayout后面.您可以将LinearLayout稍微透明一些,以便在滚动时获得不错的效果.

The ScrollView also has a padding which is equal to the size of the footer View and has clipToPadding set to false. This means the ScrollView can use paddings to display content. You need this or otherwise the bottom part of your layout would be hidden behind the LinearLayout at the bottom. You can make the LinearLayout a little transparent for a nice effect when scrolling.

这篇关于在键盘上方显示视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆