【问题标题】:Bottom Sheet Fragment comes up with keyboardBottom Sheet Fragment 附带键盘
【发布时间】:2016-09-02 09:23:11
【问题描述】:

我在底部工作表片段中有一个编辑文本。当焦点出现在编辑文本上时,布局会上升。我试过了

 android:windowSoftInputMode="adjustNothing"

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

这是我的底部工作表类:

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;
    }
}

初始状态

当键盘出现时

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

检查布局

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

【问题讨论】:

  • 您是否以编程方式尝试过?发布屏幕截图以获得更多想法。
  • 你能给我你的底片片段的代码吗?我想和你在上面实现的键盘上方的底片一样。@DKV

标签: android bottom-sheet


【解决方案1】:

在您的Dialog Fragment 中使用它。

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

onCreateView 里面是这样的。

@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:

我对您使用的layout 进行了一些更改,使其适用于您当前的layout

这里是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:

您可以尝试 android:elevation="50dp" 属性为 Bottom Sheet 上方的阴影在 frame layout 中尝试。

【讨论】:

  • @VV 发布您的对话片段代码,并附上重现确切问题的屏幕截图。告诉我你的onCreateDialog 方法。
  • @VV 发布您的 代码 和屏幕截图以获得确切的问题。没有它,没有人可以指导您解决这个问题。
  • 请检查更新。让我知道您需要更多详细信息。
  • @VV 你能告诉我你的layout content_dialog_bottom_sheet 包含什么吗?
  • @jaydroider:请检查布局
【解决方案2】:

BottomSheetFragment 的 100% 工作公式

在 BottomSheetFragment 的 onCreateDialog 中使用它

KeyboardUtil(getActivity(), view);

片段使用

new KeyboardUtil(this, findViewById(R.id.fragment_container));

通过使用这个 Util 类

https://github.com/mikepenz/MaterialDrawer/blob/aa9136fb4f5b3a80460fe5f47213985026d20c88/library/src/main/java/com/mikepenz/materialdrawer/util/KeyboardUtil.java

图片来源:迈克彭斯

【讨论】:

  • 启用和禁用方法呢,你在哪里使用它们?
  • 这对我有帮助,我的同事推荐了你的答案。一直在互联网上寻找这个。
  • 这对我有用,但我只需要在类中替换这两行 //get screen height 并从 r int height = contentView.getHeight(); 计算可用区域的差异; int diff = (int) (height - r.bottom + r.top + decorView.getContext().getResources().getDimension(R.dimen._16dp));
【解决方案3】:

似乎旧设计版本中存在错误。 我遇到了同样的问题,但是在我升级设计版本之后,adjustnothing 就无法正常工作了。

在 gradle 中:

com.android.support:design:26.1.0

在您的 BottomSheetDialog 中:

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

【讨论】:

    【解决方案4】:

    替换你的标志

    adjustNothing

    adjustPan

    查看Official Document

    【讨论】:

      【解决方案5】:

      在清单中的活动声明中,放入以下代码行:

      android:windowSoftInputMode="stateHidden"
      android:windowTranslucentNavigation="true"
      android:windowTranslucentStatus="true" 
      

      【讨论】:

        【解决方案6】:

        获取对话框的装饰视图并将底部填充设置为高度0 或您的对话框高度。

        getDialog.getWindow.getDecorView.setpadding(0,0,0,0);
        

        【讨论】:

          【解决方案7】:

          我遇到了同样的情况,我使用 SearchView 而不是 EditText。当 BottomSheetDialog 进入隐藏状态时,键盘没有隐藏。

          只需调用这个 search_channel_template.clearFocus();

          我将分享我用来隐藏键盘的代码。

          search_channel_template = (SearchView) bottomSheetDialog.findViewById(R.id.search_channel_template);
          
          txtVw_template_headerBS_down_id.setOnClickListener(new OnSingleClickListener() {
                      @Override
                      public void onSingleClick(View v) {
                          search_channel_template.clearFocus();
                          bottomSheetDialog.getBehavior().setState(BottomSheetBehaviorv2.STATE_HIDDEN);
                      }
                  });
          

          【讨论】:

            【解决方案8】:

            如果你有以下问题

            解决方案

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

            【讨论】:

              猜你喜欢
              • 2022-12-18
              • 2022-10-18
              • 2023-01-22
              • 1970-01-01
              • 2021-10-31
              • 2021-08-13
              • 2023-03-08
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多