【发布时间】:2017-07-15 12:45:49
【问题描述】:
在我的 Android 应用程序中,对软键盘使用 adjustResize 行为至关重要。因此用户可以向下滚动到其他 UI 元素,例如“继续”按钮。
我注意到 adjustResize 仅在我在布局根元素中同时具有 Manifest 设置和 android:fitsSystemWindows="true" 时才有效。 (如有错误请指正!)
但使用android:fitsSystemWindows="true",工具栏不再位于状态栏后面。这很合理,但不是我想要的。
当工具栏位于其后面时,状态栏的颜色与我的工具栏颜色相匹配。我对android:fitsSystemWindows="true" 拥有的是一个无色状态栏和一个比我想要的低24dp 的工具栏。
为了adjustResize 键盘行为,我将放弃匹配颜色的状态栏。但我的问题是,有可能两者兼得吗? 我敢为美丽和无障碍而梦想吗?
有经验丰富的人知道设置的神奇组合吗? 或者,作为一种变通方法,一种为状态栏显式着色的方法?
仅供参考:
这些是带有RelativeLayout根元素的Activity,其中一些有ListViews和EditTexts。
工具栏是android.support.v7.widget.Toolbar
可能相关的样式项目:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowContentOverlay">@null</item>
PS - 我已经阅读了几十个关于软键盘行为的类似问题,但找不到对工具栏的意外影响有帮助的任何内容。反之亦然,有很多关于工具栏/状态栏行为的样式问题,但似乎没有任何相关性。无论如何,如果我错过了什么,对不起!
非常感谢!
编辑
我一直在尝试删除 android:fitsSystemWindows="true" 并添加更多 ScrollView 或尝试将所有内容放入同一个 ScrollView。这没有任何作用。
如果我删除android:fitsSystemWindows="true",那么 UI 的底部会“粘”到屏幕底部——它不会像我期望的那样“调整大小”而是粘到软键盘的顶部在清单中设置adjustResize。
在根视图中设置android:fitsSystemWindows="true" 会使UI 像我预期的那样调整大小——但它也使工具栏不再绘制在状态栏后面。
所以我仍然是我开始的地方:(
添加布局 XML 代码示例:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- CoordinatorLayout because this view uses SnackBars -->
<!-- Relative Layout to lock "continue" button bar to bottom -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Main content, that scrolls with or without keyboard -->
<!-- Correctly sits behind transparent Status Bar -->
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/footer_persistent_height">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- ACTUAL VIEWS DELETED FOR BREVITY / CLARITY -->
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<!-- Bottom nav bar -->
<!-- Correctly sits at bottom of UI when keyboard is not visible -->
<!-- PROBLEM: Not accessible when soft keyboard is visible -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="?android:attr/buttonBarStyle">
<Button
android:id="@+id/skip_button"
android:theme="@style/ButtonContinueGrey"
android:onClick="skipClickHandler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
style="?android:attr/buttonBarButtonStyle"/>
<Button
android:id="@+id/button_progress"
android:theme="@style/ButtonContinueColored"
android:onClick="continueClickHandler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
style="?android:attr/buttonBarButtonStyle"/>
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
【问题讨论】:
-
我无法正确理解您的问题。滚动当前视图时是否要隐藏工具栏?您使用 adjustResize 行为的目的是使您的“继续”按钮可访问,您是否尝试将 RealtiveLayout 嵌套在 NestedScrollView 中?您可以通过在主题中设置 ColorPrimaryDark 来控制状态栏颜色,如果您是 >= Lollipop
-
尽量让你的布局保持在滚动视图中并移除 android:fitsSystemWindows="true" 并且只使用调整大小
-
fitsSystemWindow 只是在状态栏后面绘制您的视图。可能重复? stackoverflow.com/questions/37454733/…你能发布一个示例布局吗 - 有很多组合可能会导致问题。
-
@VarunJain 如果你能快速发表你的评论和回答,我会给你赏金! (不到一个小时就会过期)
-
我也遇到了同样的问题,你有没有找到不涉及着色状态栏的解决方案(我试图在状态栏后面绘制的不是纯色)
标签: android android-layout keyboard android-softkeyboard android-styles