软键盘隐藏了工具栏

10

我在我的布局中使用了工具栏。每当我打开活动并聚焦于编辑文本时,它会隐藏工具栏。请参见下面的图片。

软键盘弹出之前

软键盘弹出之后

以下是我的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sampleactivity">

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

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="App"
        android:textColor="@color/white"
        android:textSize="24sp" />

    <Button
        android:id="@+id/past_locations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Next"
        android:textColor="@color/white"
        style="?android:attr/borderlessButtonStyle"/>

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

<LinearLayout
    android:id="@+id/mapLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>


    <EditText
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Title"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:singleLine="true"
        />

    <AutoCompleteTextView
        android:id="@+id/tags"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:maxLines="3"
        />

</LinearLayout>

即使使用了android:windowSoftInputMode="adjustPan"也没有帮助。

我该如何避免在键盘打开时隐藏我的工具栏。


简单易行的解决方案:https://queception.com/question.php?question=110 - Stack Overflow
2个回答

23
在AndroidManifest.xml中添加android:windowSoftInputMode="adjustResize"解决了我的问题,就像这样:
 <activity
        android:name=".MainActivity"
        android:label="@string/activity_main"
        android:windowSoftInputMode="adjustResize">
 </activity>

在这种情况下,输入文本被隐藏在键盘下面。 - user924

2
我建议您为工具栏创建不同的布局,并将其包含在您的活动中。
创建toolbar.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="4dp"
    android:background="@color/color_theme"
    android:minHeight="?attr/actionBarSize">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="App"
        android:textColor="@color/white"
        android:textSize="24sp" />

    <Button
        android:id="@+id/past_locations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Next"
        android:textColor="@color/white"
        style="?android:attr/borderlessButtonStyle"/>

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

您的活动(假设为activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.sampleactivity">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />
</LinearLayout>

<LinearLayout
    android:id="@+id/mapLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>


    <EditText
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Title"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:singleLine="true"
        />

    <AutoCompleteTextView
        android:id="@+id/tags"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:maxLines="3"
        />

</LinearLayout>

如果您想尝试,请执行以下操作,看看它是否有效!!!


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