CoordinatorLayout儿童不全屏 [英] CoordinatorLayout children are not fullscreen

查看:735
本文介绍了CoordinatorLayout儿童不全屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动这是全屏显示。这工作完全与很多的布局我都试过了,除了当 CoordinatorLayout 是根的ViewGroup 。在 CoordinatorLayout 本身具有宽度和高度设置为 match_parent ,它需要整个屏幕,因为它应该。但是,应该有大小相同的子视图 CoordinatorLayout ,如果导航栏是可见还是被解雇。

问题的插图

有没有办法使孩子的看法与 CoordinatorLayout 调整?显然 fitSystemWindows 不会改变,因为这可能是由 CoordinatorLayout 的实施引起了事情,其他 ViewGroups 工作。我试图创建自定义的行为类,但我没有成功这一点。

我用这个code,使我的活动全屏:

  @覆盖
保护无效onResume(){
    super.onResume();
    INT uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    。getWindow()getDecorView()setSystemUiVisibility(uiOptions)。
}

这是用来生成图像一个简单的布局:

 < android.support.design.widget.CoordinatorLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fitsSystemWindows =真
    机器人:背景=#F00>    < ImageView的
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:fitsSystemWindows =真
        机器人:scaleType =centerCrop
        机器人:SRC =@绘制/ background_sky_light/>< /android.support.design.widget.CoordinatorLayout>


解决方案

您也可以尝试删除的android:fitsSystemWindows =真正的从ImageView的,变 onResume()是这样的:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    ....
    mRootView =(CoordinatorLayout)findViewById(R.id.coordinatorLayout);
    ....
}公共无效hideBars(){
    getWindow()。getDecorView()。setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    mRootView.setFitsSystemWindows(假);
}

mRootView.setFitsSystemWindows(假); 是装修子视图到全屏重要

如果没有这个电话,布局将显示为如果状态栏和导航栏仍然存在。如下面的图片:

截图不setFitsSystemWindows(假)

和使用该调用:

截图与setFitsSystemWindows(假)

究其原因,我认为,就是当 fitsSystemWindows 为真, CoordinatorLayout 将预留空间状态栏并且为了导航栏与借鉴酒吧本身背景其他部件工作。但是,当我们隐藏的系统UI吧,没有必要预留空间,因此,我们需要告诉 CoordinatorLayout 来释放空间用于其他子视图。

以下是布局的截图

布局的活动:

 < android.support.design.widget.CoordinatorLayout
    机器人:ID =@ + ID / coordinatorLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=@机器人:彩色/黑白
    机器人:fitsSystemWindows =真
    工具:上下文=org.hamster.carz.MainActivity>    < android.support.design.widget.AppBarLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:主题=@风格/ AppTheme.AppBarOverlay>        < android.support.v7.widget.Toolbar
            机器人:ID =@ + ID /工具栏
            机器人:layout_width =match_parent
            机器人:layout_height =?ATTR / actionBarSize
            机器人:ATTR / colorPrimary背景=
            应用:popupTheme =@风格/ AppTheme.PopupOverlay/>    < /android.support.design.widget.AppBarLayout>    <包括布局=@布局/ frag_container/>
    <! - 只是一个空的FrameLayout - >    < android.support.design.widget.FloatingActionButton
        机器人:ID =@ + ID / FAB
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =底部|结束
        机器人:layout_margin =@扪/ fab_margin
        机器人:SRC =@的mipmap / car_add/>< /android.support.design.widget.CoordinatorLayout>

在上面的截图中的片段:

 <?XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=横向>    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_weight =2
        机器人:方向=垂直>        <按钮
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_marginBottom =2DP
            机器人:layout_weight =1
            机器人:背景=@绘制/ controller_button/>        <按钮
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_marginTop =2DP
            机器人:layout_weight =1
            机器人:背景=@绘制/ controller_button/>
    < / LinearLayout中>    <的FrameLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginTop =12dp
        机器人:layout_weight =1>        < android.support.design.widget.FloatingActionButton
            机器人:ID =@ + ID / fab_disconnect
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =CENTER_HORIZONTAL
            机器人:layout_margin =@扪/ fab_margin
            机器人:SRC =@的mipmap / ic_clear
            应用:backgroundTint =@色/ colorAccentDark/>
    < /&的FrameLayout GT;    <的LinearLayout
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =match_parent
        机器人:layout_weight =2
        机器人:方向=垂直>        <按钮
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_marginBottom =2DP
            机器人:layout_weight =1
            机器人:背景=@绘制/ controller_button/>        <按钮
            机器人:layout_width =match_parent
            机器人:layout_height =0dp
            机器人:layout_marginTop =2DP
            机器人:layout_weight =1
            机器人:背景=@绘制/ controller_button/>
    < / LinearLayout中>< / LinearLayout中>

希望这有助于。

I have an Activity which is displayed fullscreen. This works perfectly with many layouts I have tried, except for when the CoordinatorLayout is the root ViewGroup. The CoordinatorLayout itself has both width and height set to match_parent and it takes the whole screen as it should. But the child views that should have the same size as the CoordinatorLayout are laid as if the navigation bar was still visible.

Is there a way to make the child views resize with the CoordinatorLayout? Obviously fitSystemWindows does not change a thing as this is probably caused by the CoordinatorLayout implementation, other ViewGroups work well. I have tried to create custom Behavior class but I was not successful with that.

I use this code to make my Activity fullscreen:

@Override
protected void onResume() {
    super.onResume();
    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    getWindow().getDecorView().setSystemUiVisibility(uiOptions);
}

This is a simple layout used to generate the image:

<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true"
    android:background="#F00">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        android:src="@drawable/background_sky_light" />

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

解决方案

You may try removing android:fitsSystemWindows="true" from ImageView, and change onResume() like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ....
    mRootView = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
    ....
}

public void hideBars() {
    getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    mRootView.setFitsSystemWindows(false);
}

The mRootView.setFitsSystemWindows(false); is important for fitting child views into full screen.

Without this call, the layout would appear as if the status bar and navigation bar still exists. As shown in the picture below:

Screenshot without setFitsSystemWindows(false)

And with this call:

Screenshot with setFitsSystemWindows(false)

The reason, I think, is that when fitsSystemWindows is true, the CoordinatorLayout will reserve space for status bar and navigation bar in order to work with other widgets which draw background of bars themselves. But when we hide the System UI bars, there is no need to reserve space, and thus we need to tell CoordinatorLayout to release the spaces for other child views.

Following is the layouts in the screenshot

Layout for the activity:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/coordinatorLayout"
    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:background="@android:color/black"
    android:fitsSystemWindows="true"
    tools:context="org.hamster.carz.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/frag_container"/>
    <!-- Just a empty FrameLayout -->

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@mipmap/car_add" />

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

The fragment in the screenshots above:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="2dp"
            android:layout_weight="1"
            android:background="@drawable/controller_button"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="2dp"
            android:layout_weight="1"
            android:background="@drawable/controller_button"/>
    </LinearLayout>

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_weight="1">

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_disconnect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_margin="@dimen/fab_margin"
            android:src="@mipmap/ic_clear"
            app:backgroundTint="@color/colorAccentDark"/>
    </FrameLayout>

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="2dp"
            android:layout_weight="1"
            android:background="@drawable/controller_button"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginTop="2dp"
            android:layout_weight="1"
            android:background="@drawable/controller_button"/>
    </LinearLayout>

</LinearLayout>

Hope this helps.

这篇关于CoordinatorLayout儿童不全屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆