将自定义视图附加到Android键盘顶部

3
什么是在Android软键盘顶部附加在XML中定义的布局的最简单方法?该视图应仅在键盘出现时显示。
3个回答

0

我认为这是更清晰的方法。

  1. 定义一个布局资源文件作为您的自定义视图

  2. 创建一个方法,该方法会填充此布局资源文件并将其作为视图返回: 代码片段

    public View returnPayKeyView(){
    View simpleView = getLayoutInflater().inflate(R.layout.simpleresource,null);

    return simpleView;

    }

  3. 使用setCandidatesView方法将此返回的视图设置为候选项

返回的视图将显示在您的键盘上方,使用填充的xml布局资源。


0
我认为最好的方法是检测键盘何时出现,然后将视图附加到屏幕底部(在调整所有布局大小后,它将位于键盘之上)。

问题在于我希望键盘浮在我的布局之上,并且不改变它(我正在使用android:windowSoftInputMode="adjustPan")。 - Daniel L.
1
那么我认为这很难做到(我不知道或想象出一种方法来做到这一点)。 - dilix

0
一种方法是将您通常的视图布局包装在FrameLayout中,并将您想要放置在键盘上方的位于框架内,与其他视图布局一起使用底部布局重力。
我的布局大致如下:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    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">

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

            <!-- all your stuff... -->

        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/stuffAboveKeyboardLayout"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="bottom"
        android:orientation="horizontal"
        android:background="#DDD">

        <!-- stuff above keyboard... -->

    </LinearLayout>

</FrameLayout>

缺点是即使键盘隐藏,它也会显示出来 - 但应该可以在键盘显示/隐藏时显示/隐藏视图:如何检测Android设备上的软件键盘是否可见?


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