在键盘上方显示按钮

3
我已经制作了一个注册表单,其中有两个按钮。现在我想要的是当键盘显示时,底部的这两个按钮应该显示在键盘上面而不是被隐藏在键盘下面。为了实现这个目标,我编写的代码没有成功。
XML代码:
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

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

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


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


                <EditText
                    android:inputType="textPersonName"
                    style="@style/EditText"
                    android:hint="@string/firstname"
                    android:id="@+id/et_first_name" />


                <EditText
                    android:inputType="textPersonName"
                    style="@style/EditText"
                    android:hint="@string/lastname"
                    android:id="@+id/et_last_name" />

                <Button
                    style="@style/EditText"
                    android:text="@string/country"
                    android:id="@+id/bt_country" />


                <Button
                    style="@style/EditText"
                    android:text="@string/gender"
                    android:id="@+id/bt_gender" />

                <EditText
                    style="@style/EditText"
                    android:inputType="phone"
                    android:hint="@string/mobileno"
                    android:id="@+id/et_mobile_no" />

                <EditText
                    style="@style/EditText"
                    android:inputType="textEmailAddress"
                    android:hint="@string/email"
                    android:id="@+id/et_email" />


            </LinearLayout>


        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            style="@style/EditText"
            android:id="@+id/bt_reg_cancel"
            android:text="@string/cancel"
            android:layout_gravity="bottom"
            android:layout_weight="1" />

        <Button
            style="@style/EditText"
            android:text="@string/save"
            android:id="@+id/bt_reg_save"
            android:layout_gravity="bottom"
            android:layout_weight="1" />
    </LinearLayout>


</LinearLayout>

请帮忙。

为什么不使用滚动视图? - Umair
将您的按钮放在ScrollView中并尝试...... - M D
我想要保持按钮的位置固定。 - Anuj
在 Android 的清单文件中,你可以使用 android:windowSoftInputMode="adjustResize" 参数来声明你的 Activity 窗口软键盘的调整方式。 - Techfist
2个回答

2
在Android清单文件中,添加属性android:windowSoftInputMode="adjustResize"。这将在键盘显示时调整您的布局大小。
因此,如果某些内容位于底部,则一旦键盘显示,它将显示在其上方。
"请问您能用相对布局展示代码吗?这会非常有帮助???- user3917131 7分钟前"
解决方案:-
1))将底部布局放在ScrollView本身中。即使键盘显示,您也可以滚动它。
2))权重方法:- ScrollView->0.7 RelativeLayout->0.3->在其中编写您的底部布局,并将属性align parent bottom true赋予它。 在此方法中,根据权重指南,您的ScrollView仅占屏幕的0.7,其余部分由底部布局占据。
3))删除ScrollView,改为LinearLayout。将顶级父RelativeLayout设置为属性align parent bottom true,将此属性设置为您的底部布局和LinearLayout的layout above属性。

由于您的整个布局在scrollview中,所以它无法工作。因为您可以滚动,所以它不会重新调整大小。移除scrollview它就能工作了。 - Rahul Gupta
整个布局不在滚动视图中。 - Anuj
在编译时,无法知道您的布局是否在底部。它仅出现在底部是因为您的滚动视图中有许多子项。如果您的滚动视图中只有一个子项,则底部布局不会出现在底部。要么将主父相对布局设置为 align parent bottom true。 - Rahul Gupta
我修改了我的回答并提供了三种方法,但没有提供完整的代码。 - Rahul Gupta

0

例如,您应该将ScrollView的layout_weight设置为1。并将LinearLayout的layout_height设置为"wrap_content"。最好将ScrollView的layout_height设置为0dp,这样LinearLayout就可以管理它的高度。

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">

    <ScrollView
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </ScrollView>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            style="@style/EditText"
            android:id="@+id/bt_reg_cancel"
            android:text="@string/cancel"
            android:layout_gravity="bottom"
            android:layout_weight="1" />
        <Button
            style="@style/EditText"
            android:text="@string/save"
            android:id="@+id/bt_reg_save"
            android:layout_gravity="bottom"
            android:layout_weight="1" />
    </LinearLayout>


</LinearLayout>

这应该就可以了。


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