CoordinatorLayout儿童不是全屏

时间:2015-08-04 09:54:36

标签: android android-layout android-design-library android-coordinatorlayout

我有一个Activity全屏显示。这与我尝试过的许多布局完美配合,除了CoordinatorLayout是根ViewGroup的时候。 CoordinatorLayout本身的宽度和高度都设置为match_parent,并且整个屏幕都应该如此。但是,应该具有与CoordinatorLayout相同大小的子视图,就像导航栏仍然可见一样。

Illustration of the problem

有没有办法让CoordinatorLayout调整子视图的大小?显然fitSystemWindows不会改变事物,因为这可能是由CoordinatorLayout实施引起的,其他ViewGroups效果很好。我曾尝试创建自定义Behavior类,但我没有成功。

我使用此代码制作Activity全屏:

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

2 个答案:

答案 0 :(得分:3)

如果您使用的是coodinatorlayout,则将android:fitsSystemWindows更改为false android:fitsSystemWindows="false

答案 1 :(得分:0)

我有类似的问题,不得不破解一些解决方案。我的CoordinatorLayout子视图不是ImageView,而是用于流式传输视频。由于我在旋转期间流式传输视频,因此我需要注意configChanges并覆盖onConfigurationChanged。如果您不想覆盖onConfigurationChanged,这可能对您不起作用,但它可能会有用。就像我说的那样,它有点像黑客,但对我有用。

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

@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]);
    }
}