当软键盘显示时,垂直滚动整个视图。

4
我正在处理一个包含TableLayout的LinearLayout屏幕,以显示文本、一些EditText和一些按钮,但在智能手机上,当我点击位于布局顶部的EditText时,位于底部的按钮会被软键盘隐藏。唯一的解决方法是隐藏软键盘,使按钮重新出现。是否有可能使我的布局在软键盘上方滚动到视图的底部,这样用户就可以看到软键盘和底部的按钮?我尝试使用ScrollView将TableLayout包围起来,但它无法滚动到布局底部,因此用户无法看到底部。以下是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">

        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
                <LinearLayout
                    android:id="@+id/linearLayoutMain"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:orientation="vertical" >

                    <include layout="@layout/login_container"/>

                    <LinearLayout 
                         android:orientation="horizontal"
                         android:background="@drawable/_grey_rounded_bottom"
                         android:layout_width="match_parent"
                         android:layout_height="30dp"/>

                </LinearLayout> 
        </ScrollView>  
</LinearLayout>

Thanks in advance.

3个回答

0

我有一个类似的视图,而且它对我来说可以运行。看看我的代码:

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

    <LinearLayout ... /> <!--Have an image that I want to stay on the top -->

    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" ... >

        <RelativeLayout ... >
            <!-- EditText of username and EditText of password and a CheckBox -->
            <LinearLayout 
                android:id="@+id/linearLayoutEntrar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/checkBoxRecordarDatos"
                android:gravity="center"
                android:layout_marginTop="7dp"
                android:weightSum="1.0">

                <Button
                    android:id="@+id/buttonEntrar"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:text="@string/login_entrar"
                    android:textColor="@android:color/white"
                    android:textStyle="bold"
                    android:textSize="@dimen/textSize"
                    android:onClick="entrar" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/linearLayoutJugar"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/linearLayoutEntrar"
                android:layout_marginTop="7dp"
                android:gravity="center"
                android:weightSum="1.0" >

                <Button
                    android:id="@+id/buttonJugar"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:onClick="jugar"
                    android:text="@string/login_jugar"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/textSize"
                    android:textStyle="bold" />

            </LinearLayout>
        </RelativeLayout>
    </ScrollView>
</LinearLayout>

尝试在每个布局上更改高度。如果不成功,我认为问题可能出在TableLayout上,请像我一样不要使用它。


我尝试改变高度,但没有起作用。我认为TableLayout没问题,因为我像你使用的按钮一样改变了它,但是也没有起作用。 - steve_patrick
尝试不要使用TableLayout。你可以使用每行一个LinearLayoutLabel + EditText)的方式。 - Lyd
我将TableLayout更改为包含按钮的LinearLayouts(就像您上面使用的那些),并且没有滚动到屏幕底部... - steve_patrick

0
在清单文件中的活动标签中添加android:windowSoftInputMode="stateVisible|adjustPan"应该可以解决问题。

0
  1. 尝试 android:windowSoftInputMode="adjustResize"
  2. 如果scrollView有兄弟视图元素,则在ScrollView上尝试以下内容

    android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"


不适用于片段调整大小,我使用了getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 但似乎没有起作用。 - user3402040

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