软键盘隐藏了Actionbar

26

我有一个简单的活动,包含一个文本视图和两个编辑框。当我开始输入时,软键盘会将布局推上去并隐藏操作栏,导致我无法选择/复制/粘贴文本。

我的活动看起来像这样:

Normal activity view

当我开始输入时,操作栏被隐藏了,就像这样——更重要的是,请注意即使文本已经被高亮选中,但正常的复制粘贴栏也消失了:

Missing action bar and the copy/paste bar

我该怎么做才能确保操作栏不会被隐藏?

  • 我尝试过一些其他SO上的提示(例如使用滚动视图),但没有一个有效。
  • 该应用程序是使用Android Studio 1.4.1向导和抽屉式活动创建的。
  • 在我的活动清单中,我使用了adjustPan。我不想使用adjustResize。

    android:windowSoftInputMode="stateAlwaysHidden|adjustPan"

我的活动在这里:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/navigation_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/app_theme.app_bar_overlay">

            <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/app_theme.popup_overlay" />

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

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="@dimen/activity_margin"
            android:fillViewport="true"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:background="@android:color/darker_gray"
                android:text="My holiday story here." />

            <View
                android:layout_width="match_parent"
                android:layout_height="@dimen/activity_margin" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/gap_normal"
                android:gravity="top"
                android:text="My Holiday in Europe" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:gravity="top"
                android:text="My holiday story is blah blah blah blah blah blah blah blah blah..." />

        </LinearLayout>


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

    <com.magicparcel.app.mysampleapp.ui.CustomNavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/navigation_drawer_header"
        app:itemIconTint="@color/app_grey_500"
        app:menu="@menu/drawer_main" />

</android.support.v4.widget.DrawerLayout>

尝试这个:https://dev59.com/Mmsy5IYBdhLWcg3w6yUN - Mohammad Hossein Gerami
谢谢,但我需要使用 adjustPan,而不是 adjustResize。 - henrykodev
你尝试过将除了AppBarLayout之外的所有视图都包装在ScrollView中吗? - Ali Bdeir
3个回答

6
在工具栏中添加app:layout_collapseMode="pin"以实现折叠。
<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/app_theme.popup_overlay" />

要在EditText中启用复制/粘贴功能,请使用android:textIsSelectable="true"。更多信息请参见https://developer.android.com/guide/topics/text/copy-paste.html


你好,只是为了澄清一些事情。假如在EditText字段上方有其他视图要怎么办呢?例如,我们在EditText上面有几个布局,并且想使用"adjustPan"属性。在这种情况下,当我们选择EditText时,所有在它上面的视图都会往上移动,其中一些(如ActionBar)会隐藏起来。我应该怎么做才能保留EditText上方的一些布局不受影响,同时让其他视图往上移动呢? - GensaGames
我不明白你的意思。你是想要保存EditText上方的一些布局,让其他布局向上移动吗?你应该怎么做呢? - Uttam Panchasara
对我不起作用,我正在使用相对布局作为根和工具栏。 - AllwiN

2

步骤1:从每个视图中删除fitsSystemWindow属性,并将其添加到清单文件中的活动标记中。

步骤2:从您的线性布局中删除此行:app:layout_behavior="@string/appbar_scrolling_view_behavior"。这是实际上将操作栏推到上面的代码。

这两个步骤解决了我的问题,希望对您有所帮助。


0

将您的工具栏添加到AppBarLayout中,并添加AppBarOverLay和PopOverlay主题。

<android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

styles.xml

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

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