【问题标题】:Toolbar resizes til softkeyboard when editing EditText编辑 EditText 时工具栏调整大小直到软键盘
【发布时间】:2015-03-24 03:02:53
【问题描述】:

这是我在这里的第一个问题,经过 2 小时搜索类似案例后,我找不到此帖子的问题。

问题是我有一个带有工具栏(v7 支持工具栏)和导航抽屉的活动。这一切都完美无缺。主要活动有一个片段容器,我在其中放置不同的片段(通过导航抽屉)。在我将 EditText 添加到片段之前,一切都运行良好。当我选择 EditText 输入一些文本时,显示软键盘并且工具栏调整大小直到底部与软键盘对齐(实际高度增加),我还不能发布图像,所以我希望这足够详细。没有选择输入的片段正常显示。 EditText 实际上在 softKeyboard 下方(至少在那里可以看到选择指示器)。

这是来自 mainactivity 的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    android:background="@color/main_color_500">


    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:paddingTop="0dp"
            android:paddingBottom="0dp"
            android:orientation="vertical"
            tools:context="com.joost.smartplanner.MainFragmentActivity">

            <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/main_color_500"
                android:fitsSystemWindows="true"
                android:elevation="4dp"
                android:id="@id/app_bar"
                app:theme="@style/CustomToolbarStyle">

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

            <FrameLayout
                android:id="@+id/mainFragmentContainer"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@color/grey_100"
                android:padding="@dimen/zero_padding"
                android:gravity="top"
                android:orientation="horizontal"
                android:focusable="true">

            </FrameLayout>
        </LinearLayout>

        <fragment
            android:id="@+id/fragment_navigation_drawer"
            android:layout_width="@dimen/nav_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"
            android:name="com.joost.smartplanner.NavigationDrawerFragment" />

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

</LinearLayout>

包含editText的片段:

<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:background="@color/fragment_bg_white"
    android:focusable="true"
    tools:context="com.joost.smartplanner.CreateEventFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/create_event_scrollview">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/divider"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"/>

            <!-- Event name part -->
            <EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:imeOptions="flagNoExtractUi"
                android:hint="Event name"
                android:padding="8dp"
                android:ems="8"
                android:focusable="true"
                android:clickable="true"
                android:id="@+id/editText" />


        </LinearLayout>
    </ScrollView>
</LinearLayout>

最后是 MainActivity onCreate 中的工具栏启动代码:

//Toolbar initiation
        toolbar  = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

            }
        });
        toolbar.inflateMenu(R.menu.menu_main);

我试图解决这个问题的一件事是为工具栏设置一个固定的高度,而不是 wrap_content 值。这部分解决了工具栏保持相同高度但工具栏内的图标消失的问题,并且在我看来,硬编码通常可以正常工作的高度不是正确的解决方案。

【问题讨论】:

    标签: android android-fragments android-edittext android-support-library android-toolbar


    【解决方案1】:

    您在此处看到的问题与工具栏中的 android:fitSystemWindows="true" 属性有关。

    看看Android appcompat toolbar stretches when searchview gets focus。似乎如果您使用该属性,父视图也应该设置android:fitSystemWindows="true"

    【讨论】:

    • 经过一个小时的额外搜索,我刚刚找到了相同的线程。谢谢!
    • @Ertyguy 这是我的布局 xml 文件pastebin.com/ZL4t5iuV,请告诉我我需要在哪里使用 android:fitSystemWindows="true" 属性。我很困惑,我在哪里使用它,当我的 autocompleteTextview 获得焦点时,工具栏仍然隐藏,即它正在将工具栏从屏幕上推出
    • @KK_07k11A0585In android:id="@+id/container_toolbar" LinearLayout
    • android:fitSystemWindows="true" 不存在。你拼写正确吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2015-11-04
    • 2019-03-19
    相关资源
    最近更新 更多