让布局知道键盘是否可见
Make layout aware whether keyboard is visible or not
我试图让布局知道软键盘是否可见,因为它覆盖了 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>
您需要查看清单中的 android:windowSoftInputMode 设置。
The adjustment made to the activity's main window — whether it is
resized smaller to make room for the soft keyboard or whether its
contents pan to make the current focus visible when part of the window
is covered by the soft keyboard.
android:windowSoftInputMode 对我不起作用,因为我使用的是 LinearLayout(在背景中重叠 RelativeLayout,但是问题中没有显示)。我必须做的是:
- 将最外面的 LinearLayout 更改为 RelativeLayout
- 将两个内部 LinearLayout 都放在 ScrollView 中(并将下方的 LinearLayout 设为 RelativeLayout 以解决一些与移动相关的布局问题)
- 将
getDialog().getWindow().setSoftInputMode((WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE));
添加到片段onCreateView
清单可以使用此解决方案保持不变。
我试图让布局知道软键盘是否可见,因为它覆盖了 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>
您需要查看清单中的 android:windowSoftInputMode 设置。
The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.
android:windowSoftInputMode 对我不起作用,因为我使用的是 LinearLayout(在背景中重叠 RelativeLayout,但是问题中没有显示)。我必须做的是:
- 将最外面的 LinearLayout 更改为 RelativeLayout
- 将两个内部 LinearLayout 都放在 ScrollView 中(并将下方的 LinearLayout 设为 RelativeLayout 以解决一些与移动相关的布局问题)
- 将
getDialog().getWindow().setSoftInputMode((WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE));
添加到片段onCreateView
清单可以使用此解决方案保持不变。