使布局了解键盘是否可见

时间:2015-07-28 14:58:55

标签: android android-layout android-softkeyboard

我试图让布局知道软键盘是否可见,因为它覆盖了部分UI。当键盘出现并使其可滚动时向上移动UI将是更可取的。

这可能吗,如果可以,怎么做?

没有键盘的视图图片 nokeyboard

键盘视图图片 with keyboard

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/dialog_min_width"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/spacing_l"
    android:background="@color/white"
    android:minWidth="@dimen/dialog_min_width"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:nestedScrollingEnabled="false">

        <LinearLayout
            android:id="@+id/dialog_content"
            style="@style/DialogContent">

            <TextView
                android:id="@+id/dialog_title"
                style="@style/DialogTitle"
                android:text="@string/label_start_round"
                />

            <TextView
                style="@style/InputLabel"
                android:text="@string/label_player_name"
                />

            <EditText
                android:id="@+id/player_name"
                style="@style/Widget.AppCompat.EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textAutoCorrect"
                />

            <TextView
                style="@style/InputLabel"
                android:text="@string/label_player_handicap"
                />

            <EditText
                android:id="@+id/player_handicap"
                style="@style/Widget.AppCompat.EditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="numberDecimal"
                />

            <TextView
                style="@style/InputLabel"
                android:text="@string/label_gender"
                />

            <Spinner
                android:id="@+id/spinner_gender"
                style="@style/Widget.AppCompat.Spinner.Underlined"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                style="@style/InputLabel"
                android:text="@string/label_selected_tee"
                />

            <Spinner
                android:id="@+id/player_tee"
                style="@style/Widget.AppCompat.Spinner.Underlined"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/dialog_footer"
        style="@style/DialogFooter">

        <TextView
            android:id="@+id/button_cancel"
            style="@style/DialogButton"
            android:text="@string/label_cancel"
            />

        <TextView
            android:id="@+id/button_start"
            style="@style/DialogButton"
            android:text="@string/label_start"
            />
    </LinearLayout>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您需要查看清单中的android:windowSoftInputMode设置。

  

对活动的主窗口进行了调整 - 是否是   调整大小调整为软键盘腾出空间或是否为它   内容平移以使当前焦点在窗口的一部分可见   被软键盘覆盖。

答案 1 :(得分:0)

android:windowSoftInputMode对我来说不起作用,因为我使用的是LinearLayout(在背景中重叠RelativeLayout,但问题中没有显示)。我必须做的是:

  1. 将最外面的LinearLayout更改为RelativeLayout
  2. 将两个内部LinearLayouts放在ScrollView中(并使较低的LinearLayout成为RelativeLayout以修复与移动相关的一些布局问题)
  3. getDialog().getWindow().setSoftInputMode((WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE));添加到片段onCreateView
  4. 使用此解决方案,清单可以保持不变。