【问题标题】:Android Linear Layout weight and soft keyboard issueAndroid 线性布局权重和软键盘问题
【发布时间】:2017-06-13 21:17:19
【问题描述】:

我正在尝试在聊天应用中实现以下视图。基本上有两种状态,一种显示软触摸键盘,一种没有显示。

所以这是我没有显示键盘的初始状态。

这就是键盘出现时发生的情况。

这就是我想要实现的目标。

注意 我目前正在使用“调整调整大小”作为 windowSoftInputMode。我知道使用“adjust-pan”可以解决问题,但是使用“adjust-pan”有两个问题:

  1. 工具栏也会向上移动,为编辑文本和键盘腾出空间。
  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 或根标签吗?
  • 发布您的代码。
  • 大家好,感谢您的快速回复,我已编辑问题以包含我的 XML。这是我的布局的粗略草图。
  • 试试这个answer
  • @kishorejethava 将根布局属性“isScrollContainer”设置为“false”不起作用,因为我使用的是线性布局,并且它仍然尊重权重分布。

标签: android android-layout android-linearlayout android-softkeyboard


【解决方案1】:

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

windowSoftInputMode="adjustResize" - Activity 的主窗口总是调整大小,以便为屏幕上的软键盘腾出空间。此外,adjustResize 将 ToolBar 保持在顶部。

尝试在 NestedScrollView 中添加两个 LinearLayout。我之所以这么说是因为您的 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-28
    • 2011-10-17
    相关资源
    最近更新 更多