在对话框片段中,Autocompletetextview下拉菜单出现在软键盘后面

13

我的应用中有一个对话框片段,其中包含一个autocompletetextview。但是下拉列表没有与软键盘的顶部对齐,而是被放置在后面,导致无法访问某些项目。

以下是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="10dp"
    android:orientation="horizontal"
    android:windowSoftInputMode="adjustPan|adjustResize">

    <android.widget.AutoCompleteTextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/customDialogAtocompleteTextview"
        android:layout_weight=".7"
        android:layout_gravity="top" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".3">
        <Button
            android:id="@+id/customDialogBtOk"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Aceptar"/>
        <Button
            android:id="@+id/customDialogBtSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Buscar"/>
        <Button
            android:id="@+id/customDialogBtMore"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Mas"/>
    </LinearLayout>

</LinearLayout>

那么我该如何使它与键盘对齐呢?

2个回答

29
理论上,android:windowSoftInputMode="adjustPan|adjustResize"应该可以实现此功能,但出于某些原因它却不能。因此,你需要通过编程来实现相同的效果:
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

并且魔法发生了!!!


你能详细解释一下答案吗?它太简洁了,我不知道如何获取自动完成文本视图的对话框。 - Abhishek Mathur
1
这个建议不起作用。TextView不再被遮挡,但下拉菜单也被阻挡了。 - FractalBob

5

[更新] 在我的情况下,我使用了带有自动完成的bottomShetDialogFragment。由于我没有为自动完成设置dropdownanchor,所以下拉菜单没有显示。 只需添加android:dropDownAnchor="@id/layout_above_this_autocomplete"就可以完美解决。

在您的style.xml中添加新样式。

<style name="AppBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="bottomSheetStyle">@style/AppModalStyle</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowSoftInputMode">adjustResize</item>
</style>
<style name="AppModalStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="behavior_peekHeight">400dp</item>

</style>

在你的底部对话框片段中。 onCreate -> setStyle(DialogFragment.STYLE_NORMAL, R.style.AppBottomSheetDialogTheme)
override fun getTheme(): Int {
    return R.style.AppBottomSheetDialogTheme
}

result


谢谢您!我试了十几个方法花了一个小时,这个是解决方案。 - Eman

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