如何在键盘打开时调整底部表单大小?

huangapple 未分类评论47阅读模式
英文:

how to resize bottomsheet when keyboard is opened?

问题

我使用了一个BottomSheet,我已经定义为STATE_EXPANDED。在底部表单中,我有3个编辑框。我在底部表单打开时强制打开了编辑框。当底部表单打开时,键盘同时打开。到目前为止都没有问题。我在LinearLayout中使用了android:windowSoftInputMode="stateVisible|adjustResize"。当底部表单打开时,会出现以下图像:

图片描述

但是我的目标是这样的:

图片描述

我指的是,当底部表单打开时选择编辑框时,我希望底部的空间能够与键盘的高度一样,以便菜单出现。

以下是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:background="?attr/bottombarcolor"
    android:windowSoftInputMode="stateVisible|adjustResize"
    android:fitsSystemWindows="true">

    <!-- 这里放置了其他的布局元素 -->

</LinearLayout>

当我点击底部的编辑框时,我想要的情况发生了。我希望当选择第一个编辑框时也能发生这种情况。

英文:

I am using one BottomSheet which I have defined as STATE_EXPANDED.I have 3 edittext in bottomsheet.I did force edittext when bottomsheet opened.When bottomsheet is opened keyboard is opened at the same time.so far no problem.im using android:windowSoftInputMode="stateVisible|adjustResize" in Linearlayout.When bottomsheet is opened, an image like this occurs;

enter image description here

but my goal is here;

enter image description here

I mean, when edittext is selected when botttomsheet is opened, I want the space at the bottom to be as much as the keyboard for the menu to appear.

here are my codes;

&lt;LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:padding=&quot;16dp&quot;
android:gravity=&quot;center_horizontal&quot;
android:orientation=&quot;vertical&quot;
android:background=&quot;?attr/bottombarcolor&quot;
android:windowSoftInputMode=&quot;stateVisible|adjustResize&quot;
android:fitsSystemWindows=&quot;true&quot;&gt;

  &lt;LinearLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;?actionBarSize&quot;
    android:orientation=&quot;horizontal&quot;
    android:layout_marginBottom=&quot;10dp&quot;
    android:backgroundTint=&quot;?attr/colorPrimary&quot;&gt;

    &lt;com.google.android.material.button.MaterialButton
        android:id=&quot;@+id/btn_back&quot;
        style=&quot;@style/Widget.MaterialComponents.Button.TextButton.Icon&quot;
        android:layout_width=&quot;60dp&quot;
        android:layout_weight=&quot;0&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;center&quot;
        android:textColor=&quot;?attr/textcolor&quot;
        app:icon=&quot;@drawable/ic_arrow_back_black_24dp&quot;
        app:iconGravity=&quot;start&quot;
        app:iconTint=&quot;?attr/colorPrimary&quot;
        android:backgroundTint=&quot;?attr/bottombarcolor&quot;/&gt;

    &lt;com.google.android.material.textfield.TextInputLayout
        style=&quot;@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:hint=&quot;@string/dropdownlist&quot;
        app:hintTextColor=&quot;?attr/colorPrimary&quot;
        app:boxBackgroundColor=&quot;?attr/bottombarcolor&quot;
        android:layout_weight=&quot;1&quot;
        android:textColorHint=&quot;?attr/textcolor&quot;
        app:boxCollapsedPaddingTop=&quot;2dp&quot;&gt;

        &lt;AutoCompleteTextView
            android:id=&quot;@+id/filled_exposed_dropdown&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;sdfsdfsfsg&quot;
            android:inputType=&quot;none&quot;
            android:fontFamily=&quot;@font/regularopensans&quot;
            android:textColor=&quot;?attr/textcolor&quot; /&gt;

    &lt;/com.google.android.material.textfield.TextInputLayout&gt;




    &lt;com.google.android.material.button.MaterialButton
        android:id=&quot;@+id/btn_apply&quot;
        style=&quot;@style/Widget.MaterialComponents.Button.TextButton.Icon&quot;
        android:layout_width=&quot;60dp&quot;
        android:layout_weight=&quot;0&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_gravity=&quot;center&quot;
        android:textColor=&quot;?attr/textcolor&quot;
        app:icon=&quot;@drawable/ic_check_black_24dp&quot;
        app:iconGravity=&quot;end&quot;
        app:iconTint=&quot;?attr/colorPrimary&quot;
        /&gt;

&lt;/LinearLayout&gt;
&lt;View
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:layout_marginBottom=&quot;16dp&quot;
    android:background=&quot;@android:color/white&quot;
    android:backgroundTint=&quot;?attr/colorPrimary&quot;/&gt;



&lt;TextView
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginBottom=&quot;4dp&quot;
    android:text=&quot;@string/newtask&quot;
    android:textColor=&quot;?attr/textcolor&quot;
    android:fontFamily=&quot;@font/regularopensans&quot;/&gt;

&lt;com.google.android.material.textfield.TextInputLayout
    android:id=&quot;@+id/outlinedTfNew&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:hint=&quot;@string/entertask&quot;
    style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;
    app:hintTextColor=&quot;?attr/colorPrimary&quot;
    android:textColorHint=&quot;?attr/textcolor&quot;
    app:boxStrokeColor=&quot;?attr/textcolor&quot;
    app:boxStrokeWidth=&quot;0dp&quot;
    app:endIconMode=&quot;clear_text&quot;
    app:startIconDrawable=&quot;@drawable/ic_mic_black_24dp&quot;
    app:endIconTint=&quot;?attr/colorPrimary&quot;
    app:startIconTint=&quot;?attr/colorPrimary&quot;&gt;

    &lt;com.google.android.material.textfield.TextInputEditText
        android:id=&quot;@+id/txteditNew&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:textColor=&quot;?attr/textcolor&quot;
        android:fontFamily=&quot;@font/lightopensans&quot; /&gt;
    &lt;requestFocus /&gt;

&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;View
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:layout_marginBottom=&quot;16dp&quot;
    android:background=&quot;@android:color/white&quot;
    android:backgroundTint=&quot;?attr/textcolor&quot;/&gt;

&lt;TextView
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:text=&quot;@string/taskcompdate&quot;
    android:textColor=&quot;?attr/textcolor&quot;
    android:textAlignment=&quot;center&quot;
    android:fontFamily=&quot;@font/regularopensans&quot;/&gt;

&lt;com.google.android.material.textfield.TextInputLayout
    android:id=&quot;@+id/outlinedTfComp&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:hint=&quot;@string/nodate&quot;
    style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;
    app:hintTextColor=&quot;?attr/colorPrimary&quot;
    android:textColorHint=&quot;?attr/textcolor&quot;
    app:boxStrokeColor=&quot;?attr/textcolor&quot;
    app:boxStrokeWidth=&quot;0dp&quot;
    app:endIconMode=&quot;clear_text&quot;
    app:startIconTint=&quot;?attr/colorPrimary&quot;
    app:startIconDrawable=&quot;@drawable/ic_today_black_24dp&quot;
    app:endIconTint=&quot;?attr/colorPrimary&quot;&gt;

    &lt;com.google.android.material.textfield.TextInputEditText
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:textColor=&quot;?attr/textcolor&quot;
        android:fontFamily=&quot;@font/lightopensans&quot;/&gt;

&lt;/com.google.android.material.textfield.TextInputLayout&gt;
&lt;View
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:layout_marginBottom=&quot;16dp&quot;
    android:background=&quot;@android:color/white&quot;
    android:backgroundTint=&quot;?attr/textcolor&quot;/&gt;

&lt;TextView
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:text=&quot;@string/tasktime&quot;
    android:textColor=&quot;?attr/textcolor&quot;
    android:fontFamily=&quot;@font/regularopensans&quot;
    android:textAlignment=&quot;center&quot;/&gt;

&lt;com.google.android.material.textfield.TextInputLayout
    android:id=&quot;@+id/outlinedTfTime&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:hint=&quot;@string/notime&quot;
    style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;
    app:hintTextColor=&quot;?attr/colorPrimary&quot;
    android:textColorHint=&quot;?attr/textcolor&quot;
    app:boxStrokeColor=&quot;?attr/textcolor&quot;
    app:boxStrokeWidth=&quot;0dp&quot;
    app:endIconTint=&quot;?attr/colorPrimary&quot;
    app:endIconMode=&quot;clear_text&quot;
    app:startIconDrawable=&quot;@drawable/ic_access_time_black_24dp&quot;
    app:startIconTint=&quot;?attr/colorPrimary&quot;&gt;

    &lt;com.google.android.material.textfield.TextInputEditText
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:textColor=&quot;?attr/textcolor&quot;
        android:fontFamily=&quot;@font/lightopensans&quot; /&gt;

&lt;/com.google.android.material.textfield.TextInputLayout&gt;


&lt;View
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;1dp&quot;
    android:layout_marginBottom=&quot;16dp&quot;
    android:background=&quot;@android:color/white&quot;
    android:backgroundTint=&quot;?attr/textcolor&quot;
    /&gt;

When I click on the bottom edittext, the situation I want occurs.I want this to happen when the first edittext is selected.

huangapple
  • 本文由 发表于 2020年4月5日 19:09:40
  • 转载请务必保留本文链接:https://java.coder-hub.com/61041615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定