在键盘顶部显示视图

时间:2023-01-30
本文介绍了在键盘顶部显示视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

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