【问题标题】:Scroll vertically through the whole view when the soft keyboard is shown显示软键盘时垂直滚动整个视图
【发布时间】:2013-07-10 09:12:41
【问题描述】:

我正在处理一个带有 LinearLayout 和 TableLayout 的屏幕,以便显示文本、一些 EditTexts 和一些按钮。

问题在于,在智能手机上,当我单击布局顶部的 EditText 时,底部的按钮隐藏在软键盘后面。使按钮再次出现的唯一方法是隐藏软键盘。

是否可以让我在软键盘顶部的布局滚动到我的视图按钮,以便用户可以看到软键盘和按钮底部?

我尝试在 TableLayout 周围使用 ScrollView,但它不会滚动到我的布局的按钮,因此用户看不到底部。

这是我的布局:

<?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>

提前致谢。

【问题讨论】:

    标签: android layout keyboard scroll


    【解决方案1】:

    我也有类似的看法,它适合我。查看我的代码:

    <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 还可以,因为我为您正在使用的按钮更改了它但没有工作。
    • 尽量不要使用TableLayout。您可以每行使用一个LinearLayout (Label + EditText)。
    • 我更改了 LineraLayouts 的 TableLayout,内部带有按钮(如您在上面使用的按钮),但没有滚动到屏幕底部...
    【解决方案2】:

    android:windowSoftInputMode="stateVisible|adjustPan" 添加到清单中的活动标签应该可以完成工作。

    【讨论】:

      【解决方案3】:
      1. 试试 android:windowSoftInputMode="adjustResize"
      2. 如果 scrollView 有兄弟视图元素,请在 ScrollView 上试试这个

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

      【讨论】:

      • 不适用于片段adjustResize,我使用getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);似乎不起作用
      猜你喜欢
      • 1970-01-01
      • 2015-10-19
      • 2012-07-03
      • 1970-01-01
      • 2016-01-08
      • 2014-11-23
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多