Translucent system bars and content margin in KitKat

怎甘沉沦 提交于 2019-12-12 07:07:56

问题


I'm trying to take advantage of the new KitKat translucent system bars, to get a full screen image in the background of my app.

I'm having problems figuring out the right settings to get the behaviour I want. Right now I have a ViewPager with three Fragments each one composed of a RelativeLayout containing an ImageView (for the background image) and a TextView for the content.

What I'm after is to have the content fit fully in the portion of the screen available for interactions, and the image to take the whole visible portion of the screen.

If I just use android:Theme.Holo.Light.NoActionBar.TranslucentDecor as my theme it looks fine in portrait, but the navigation bar overlaps the content in landscape (see the screenshots below).

Following the recommendation in the docs, I added android:fitsSystemWindows = true to my theme, but that generates some weird artifacts (see below)

How can I make it behave like in the second example, but look good, without the visual artifacts?


回答1:


I know this is old, but I've just run into the same problem and found an easy solution, so I wanted to share it in case this was run into by anyone Google'ing around.

It seems that there is an Android bug when a ViewPager (especially an ImageSwitcher) is placed within a layout that has the attribute android:fitsSystemWindows="true". For some reason the system windows seem to be getting artifacts drawn all over them. :/

Anyway, I found a fix. For my activity, I had an XML layout as follows:

<FrameLayout 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"
    <!-- NOT HERE! android:fitsSystemWindows="true" -->
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageSwitcher
        android:id="@+id/background_image_switcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:layout_margin="0dp"
        android:background="@color/background_dark_navy"
        android:inAnimation="@android:anim/fade_in"
        android:outAnimation="@android:anim/fade_out"
        >

        <ImageView
            style="@style/BlurredBackgroundImage"
            />
        <ImageView
            style="@style/BlurredBackgroundImage"
            />

    </ImageSwitcher>

    <FrameLayout
        android:fitsSystemWindows="true" <!-- Here!!! -->
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Fitted content here -->

    </FrameLayout>

The trick was to not contain the ImageSwitcher in a layout with the android:fitsSystemWindows="true" attribute, but to instead move the android:fitsSystemWindows="true" attribute to the inner FrameLayout containing the actual content I needed to fit (the title text in your case). Unfortunately, this allows for the ImageSwitcher/ViewPager's view to get slightly cut off by the system windows, but if the image is used like a background image anyway it doesn't matter too much and is a much better trade off than artifacts or maintaining all of the different dimensions/styles that may or may not have disabled navigation (such as the currently selected answer).

I hope this helps someone!




回答2:


My solution was to disable translucent navigation in landscape mode. You still get a translucent status bar, but it fixes the issue where the nav bar is opaque and overlaps in landscape.

res/values-v19/styles.xml (these values are in my theme)

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">@bool/translucentNavBar</item>

res/values-v19/bools.xml (enable translucent nav)

<bool name="translucentNavBar">true</bool>

res/values-land-v19/bools.xml (disable translucent nav in landscape)

<bool name="translucentNavBar">false</bool>

res/values-sw600dp-land-v19/bools.xml (enable translucent nav for tablets in landscape)

<bool name="translucentNavBar">true</bool>



回答3:


I've run into similar problems, but I wasn't using a ViewPager, so this may not work. I solved by doing some nesting as Gabe described: only the inner ViewGroup is fitSystemWindows="true", the outer ViewGroup which displays the background is therefore allowed to extend under the translucent bars. Then I'm also calling requestFitSystemWindows() on orientation changes. Again, your ViewPager might be complicating things and my solution won't work, but I hope this helps.




回答4:


Put another ViewGroup inside of the one with the image and fitsSystemWindows to true




回答5:


You should wrap this inside an Linear Layout and set fitsSystemWindows to true.



来源:https://stackoverflow.com/questions/20781014/translucent-system-bars-and-content-margin-in-kitkat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!