Android Coordinator布局儿童不是全屏的

Android Coordinator布局儿童不是全屏的,android,android-layout,android-design-library,android-coordinatorlayout,Android,Android Layout,Android Design Library,Android Coordinatorlayout,我有一个全屏显示的活动。这在我尝试过的许多布局中都非常有效,除了CoordinatorLayout是根ViewGroup时。CoordinatorLayout本身的宽度和高度都设置为match_parent,并且它会按照它应该的方式使用整个屏幕。但是,应该与坐标布局具有相同大小的子视图被放置在导航栏仍然可见的位置 是否有方法使用坐标布局调整子视图的大小?显然,fitSystemWindows不会改变任何事情,因为这可能是由协调器布局实现引起的,其他视图组工作正常。我曾尝试创建自定义行为类,但没

我有一个全屏显示的
活动
。这在我尝试过的许多布局中都非常有效,除了
CoordinatorLayout
是根
ViewGroup
时。
CoordinatorLayout
本身的宽度和高度都设置为
match_parent
,并且它会按照它应该的方式使用整个屏幕。但是,应该与
坐标布局
具有相同大小的子视图被放置在导航栏仍然可见的位置

是否有方法使用
坐标布局调整子视图的大小?显然,
fitSystemWindows
不会改变任何事情,因为这可能是由
协调器布局
实现引起的,其他
视图组
工作正常。我曾尝试创建自定义
行为
类,但没有成功

我使用此代码使我的
活动
全屏显示:

@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);
}
这是用于生成图像的简单布局:

<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>

我也遇到了类似的问题,不得不破解一些解决方案。我的CoordinatorLayout子视图用于流式传输视频,而不是ImageView。由于我在旋转期间流式传输视频,所以我需要查看配置更改并覆盖onConfigurationChanged。如果您不想覆盖OnConfiguration Changed,这可能对您不起作用,但它可能会起作用。就像我说的,这有点像黑客,但对我来说很有效

我最终完全展开工具栏,存储偏移量,然后将其隐藏(将其视为标准视图)。然后,当我向后旋转时,我会将其大小调整为用户旋转设备时的偏移量

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        showToolbar();
        mViewPager.setVisibility(View.VISIBLE);

    } else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        hideToolbar();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mViewPager.setVisibility(View.GONE);
    }
}

public void hideToolbar()
{
    setCurrentOffset();
    expandToolbarToHeight(0);
    findViewById(R.id.toolbar).setVisibility(View.GONE);
}

public void showToolbar()
{
    expandToolbarToHeight(oldOffset);
    findViewById(R.id.toolbar).setVisibility(View.VISIBLE);
}

public void setCurrentOffset() {
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
    if (behavior != null) {
        oldOffset = behavior.getTopAndBottomOffset();
    }
}

public void expandToolbarToHeight(int height) {
    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppBarLayout.getLayoutParams();
    AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
    if (behavior != null) {
        behavior.setTopAndBottomOffset(height);
        behavior.onNestedPreScroll(mCoordinatorLayout, mAppBarLayout, null, 0, 1, new int[2]);
    }
}

您可以尝试从ImageView中删除android:fitsSystemWindows=“true”
,并更改如下内容:

@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);
}
mRootView.setFitsSystemWindows(false)对于将子视图适配到全屏非常重要

如果没有此调用,布局将显示为状态栏和导航栏仍然存在。如下图所示:

通过这个电话:

我认为,原因是当
fitsystemwindows
为真时,
CoordinatorLayout
将为状态栏和导航栏预留空间,以便与其他绘制栏背景的小部件一起工作。但是当我们隐藏系统UI栏时,不需要保留空间,因此我们需要告诉
CoordinatorLayout
为其他子视图释放空间

以下是屏幕截图中的布局

活动的布局:

<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>

上面截图中的片段:

<?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>


希望这有帮助。

如果您使用的是coodinatorlayout,请将android:fitsSystemWindows更改为false
android:fitsystemwindows=“false