android - Bottom Sheet Fragment 附带键盘

标签 android bottom-sheet

我在 Bottom Sheet fragment 中有一个编辑文本。当焦点出现在编辑文本上时,布局会上升。我试过了

 android:windowSoftInputMode="adjustNothing"

它适用于父 Activity,但不适用于对话框 fragment 。

这是我的 Bottom Sheet 类:

public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false);
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        return v;
    }
}

初始状态

enter image description here

当键盘出现时

enter image description here

我希望布局始终保持在底部,键盘应位于布局上方。

检查布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheetLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@android:color/holo_blue_light"
android:padding="@dimen/activity_vertical_margin"
app:behavior_hideable="true"
app:behavior_peekHeight="60dp"
app:layout_behavior="@string/bottom_sheet_behavior">


<EditText
    android:id="@+id/edt"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="@android:color/white"
    android:padding="10dp" />

<TextView

    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_below="@+id/edt" />

最佳答案

Use this in your Dialog Fragment.

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Inside onCreateView like this.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.dialog_fragment, container);

        //set to adjust screen height automatically, when soft keyboard appears on screen 
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);


        return rootView;
    }

编辑 1:

I have made some changes with what layout you are using make it apply in your current layout.

这里是布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_gravity="bottom"
    android:background="@android:color/holo_blue_light"
    android:padding="10dp"
    app:behavior_hideable="true"
    app:behavior_peekHeight="60dp"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:isScrollContainer="false"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <EditText
                android:id="@+id/edt"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="@android:color/white"
                android:padding="10dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="250dp"
                android:layout_below="@+id/edt" />

        </LinearLayout>


    </ScrollView>

</FrameLayout>

这里是 fragment

public class TestFragment extends BottomSheetDialogFragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.test4, container, false);
        return v;
    }

编辑 2:

You can try android:elevation="50dp" property for shadow above Bottom Sheet give a try with that in frame layout.

enter image description here

关于android - Bottom Sheet Fragment 附带键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39288879/

相关文章:

带有 RecyclerView 的 Android BottomSheet

android - 在 Android 中将 Assets 或原始或资源文件作为 File 对象读取

java - Android - support.v7.PreferenceCategory 填满整个屏幕

android - 使用 LocationSettingsRequest 请求高精度位置不更新位置设置

android - 带有BottomSheet的windowSoftInputMode不起作用

android - 如何动态更改Modal Bottomsheet android中的内容

android - 允许 NestedScrollView 在具有自定义行为的 BottomSheet 内完全滚动

android - 尽管对象存在 Kotlin\Room,SELECT 查询返回 null

java - 错误地覆盖包

android - 如何将 bottomsheet 设置到底部导航 View 的顶部