【问题标题】:Keep toolbar fixed and other view not保持工具栏固定,其他视图不固定
【发布时间】:2016-11-29 15:11:23
【问题描述】:

我有以下布局

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:animateLayoutChanges="true">

    <android.support.design.widget.AppBarLayout
        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="wrap_content" />

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

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <!-- Content -->

    </ScrollView>

    <include layout="@layout/fixed_layout_when_keyboard_not_visible" />

</RelativeLayout>

在 AndroidManifest 中,我将 android:windowSoftInputMode 设置为 adjustResize,它不会在键盘可见时“挤压”内容,并且始终保持工具栏固定,即使在我滚动时也是如此。 但是,我在底部有另一个视图,在 ScrollView 之外,我也想修复它,但同时在键盘弹出时不阻止任何内容 - 现在它与键盘一起移动,如下所示属性layout_gravity="bottom"。

我可以在键盘显示/隐藏时切换该布局的可见性,但这似乎不是最优雅的解决方案。你有什么建议?非常感谢任何帮助:)

【问题讨论】:

  • 我相信聆听软键盘并隐藏/显示视图是实现您所需的最佳方式。 android 的默认行为是 ether 显示或隐藏整个内容。

标签: android android-layout android-softkeyboard window-soft-input-mode


【解决方案1】:

您应该通过添加android:isScrollContainer="false" 告诉ScrollView 不要注册为滚动容器来解决此问题。您的新ScrollView应该如下所示:

<ScrollView
     android:id="@+id/scroll_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:fillViewport="true"
     android:isScrollContainer="false">

        <!-- Content -->

</ScrollView>

【讨论】:

  • 不会隐藏键盘下的内容,不仅是包含的视图,还有滚动视图项
  • 好吧,您可以滚动到 ScrollView 中所需的组件,如果 OP 想要切换 ScrollView 下方组件的可见性,我只能假设它们不够重要在不显示键盘的情况下进入。
  • 嗯,我试过了,不幸的是它没有改变任何东西。 ScrollView 下方的布局仍然使用键盘移动
【解决方案2】:

试试下面的代码

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        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:orientation="vertical"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true">

        <android.support.design.widget.AppBarLayout
            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="wrap_content" />

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

        <ScrollView
            android:id="@+id/scroll_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <!-- Content -->

        </ScrollView>


    </RelativeLayout>

【讨论】:

  • 不幸的是,将 RelativeLayout 更改为 LinearLayout 并没有完成这项工作,不过感谢您的建议 :)
猜你喜欢
  • 1970-01-01
  • 2020-02-16
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 1970-01-01
  • 2017-09-17
相关资源
最近更新 更多