簡體   English   中英

Android線性布局重量和軟鍵盤問題

[英]Android Linear Layout weight and soft keyboard issue

我正在嘗試在聊天應用中實現以下視圖。 基本上有兩種狀態,一種是軟觸鍵盤顯示,另一種是沒有。

所以這是我沒有鍵盤顯示的初始狀態。

在此輸入圖像描述

這是鍵盤出現時發生的情況。

在此輸入圖像描述

這就是我想要實現的目標。

在此輸入圖像描述

注意我目前使用“adjust-resize”作為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>

所以這就是我認為你應該做的,使用

windowSoftInputMode =“adjustResize” - 活動的主窗口始終調整大小,以便為屏幕上的軟鍵盤騰出空間。 adjustResize還可以將ToolBar保持在最頂層。

嘗試在NestedScrollView中添加兩個LinearLayouts。 我之所以這樣說是因為您的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM