【问题标题】:Soft keyboard pushing toolbar into top status bar软键盘将工具栏推入顶部状态栏
【发布时间】:2016-02-18 21:42:03
【问题描述】:

我已经完成了至少十个类似的 SO 问题,但似乎没有任何效果。

我有一个简单的相对布局,带有工具栏、文本视图、编辑文本等(参见下面的 xml)。

当编辑文本被聚焦时,我需要软键盘将我的布局向上推,但在工具栏下方(即工具栏被固定),但键盘将整个布局向上推,包括工具栏?

我已经尝试过 adjustPan、adjustResize、scrollviews 等,但没有成功,我们将不胜感激。

布局xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.Test">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:id="@+id/appbar_layout"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/appbar_layout">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">



        <TextView
            android:id="@+id/tv_title"
            android:textSize="25dp"
            android:textColor="@color/colorPrimaryDark"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/iv"
            android:layout_alignParentLeft="true" />

        <TextView
            android:id="@+id/tv_date"
            android:textSize="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/iv"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tv_title"/>



        <ImageView
            android:id="@+id/iv"
            android:layout_marginRight="-6dp"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_alignParentRight="true"
            android:maxHeight="150dp"

            />


        <TextView
            android:id="@+id/tv_description"
            android:layout_marginTop="15dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/iv"
            android:layout_centerHorizontal="true"
            android:textSize="15dp"
            android:textColor="@color/colorPrimaryDark"/>

        <RatingBar
            android:id="@+id/rb"
            style="?android:attr/ratingBarStyle"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv_description"
            android:numStars="5" />

        <View
            android:id="@+id/view"
            android:layout_below="@+id/rb"
            android:layout_width="match_parent"
            android:layout_marginTop="10dp"
            android:layout_height="1dp"
            android:visibility="visible"
            android:background="@color/colorPrimary"/>

        <EditText
            android:id="@+id/et"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:padding="@dimen/activity_horizontal_margin"
            android:layout_below="@+id/view"
            android:gravity="top"
            android:background="@android:color/transparent"
            />

        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/textColorPrimary"
            android:textSize="20dp"
            android:paddingTop="10dp"
            android:paddingBottom="10dp"
            android:layout_alignParentBottom="true"
            />
    </RelativeLayout>
</ScrollView>

</RelativeLayout>

【问题讨论】:

    标签: android xml layout toolbar


    【解决方案1】:

    我在片段的 onCreateView 上使用此代码,它工作正常。

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

    【讨论】:

    • 作为一个选项,您可以将此行添加到清单中:
    【解决方案2】:

    使用 CollapsingToolbarLayout

    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    

    并确保将 app:layout_collapseMode="pin" 标志添加到 layout.xml 中的工具栏。

    【讨论】:

    • 我会试试这两个。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-03
    • 2019-05-04
    相关资源
    最近更新 更多