Android 线性布局权重和软键盘问题

4
我希望您能在聊天应用程序中拥有以下视图。基本上有两种状态,一种是显示软触摸键盘,一种是不显示。
因此,这是我的初始状态,没有显示键盘。

enter image description here

键盘出现时会发生什么。

enter image description here

这就是我想要实现的。

enter image description here

注意 我目前正在使用"adjust-resize"作为windowSoftInputMode。我知道使用"adjust-pan"可以解决问题,但是使用"adjust-pan"会出现两个问题:

  1. 工具栏也会上移以腾出空间给edit-text和键盘。
  2. editText的一部分会被键盘遮挡。

需要布局专家的帮助! 提前感谢。

编辑:

这是我的XML代码:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/view_group_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimaryDark"
            android:elevation="4dip" >

            <!-- Toolbar stuff -->

        </android.support.v7.widget.Toolbar>

    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_bar"
        android:layout_below="@+id/view_group_toolbar"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.6">

            <include
                layout="@layout/layout_that_covers_60%_of_the_screen (This is not my actual layout name :/ using it for understandability)"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/view_group_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.4"
            android:gravity="center_vertical">

            <include
                layout="@layout/layout_that_covers_40%_of_the_screen"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="60dip"
        android:layout_alignParentBottom="true"
        android:gravity="bottom"
        android:padding="8dip" > 
            <!-- This is where my edit text resides -->

    </RelativeLayout> 

</RelativeLayout>

你可以提供你的XML文件或者根标签吗?请提出一个结构良好的问题。 - Charuක
大家好,感谢您们的快速回复。我已经编辑了问题并包含了我的XML文件。这是我布局的草图。 - Tejas
尝试这个答案 - Kishore Jethava
尝试这段代码,也许会有所帮助:https://dev59.com/w1gR5IYBdhLWcg3wqOxW#41038631 - Dhara Patel
对我来说也很好用。问题是,如果您在上面看到的图像中,LinearLayout 1覆盖了屏幕的60%,则在键盘弹出后,它会缩小以占据剩余空间的60%。我希望此布局被推到可见屏幕之上,就像使用“adjustPan”一样。 - Tejas
显示剩余5条评论
1个回答

0

所以这是我认为你应该做的事情,使用

windowSoftInputMode="adjustResize" - 活动的主窗口始终会被调整大小以腾出屏幕上的软键盘空间。同时,adjustResize 会将工具栏保持在顶部。

尝试将你的两个 LinearLayout 都放在 NestedScrollView 中。我之所以这么说是因为你的 NestedScrollView 内容可以向上滚动。

我认为你的布局会像这样 - 。

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/view_group_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimaryDark"
        android:elevation="4dip" >

        <!-- Toolbar stuff -->

    </android.support.v7.widget.Toolbar>

</LinearLayout>


<NestedScrollView 
    android:layout_above="@+id/bottom_bar"
    android:layout_below="@+id/view_group_toolbar">
  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6">

        <include
            layout="@layout/layout_that_covers_60%_of_the_screen (This is not my actual layout name :/ using it for understandability)"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/view_group_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4"
        android:gravity="center_vertical">

        <include
            layout="@layout/layout_that_covers_40%_of_the_screen"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

  </LinearLayout>
</NestedScrollView>
<RelativeLayout
    android:id="@+id/bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="60dip"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:padding="8dip" > 
        <!-- This is where my edit text resides -->

</RelativeLayout> 

如果这个对你有效,请告诉我。

编辑1: 如果你的布局中包含RecyclerView,你可能需要设置这个属性来实现平滑滚动 -

recyclerView.setNestedScrollingEnabled(false);

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