成功之路必定艰辛 2021-07-12 15:38 采纳率: 100%
浏览 86
已采纳

复杂布局使用Immersionbar无法实现状态栏沉浸式,求支援!

Fragment中CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout复杂布局使用Immersionbar无法实现状态栏沉浸式。

img

<?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="vertical"
    android:background="@color/black">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/vp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
        </androidx.viewpager.widget.ViewPager>

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/center_appbar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="0dp"
            android:fitsSystemWindows="true">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/collapsing_toobar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:contentScrim="@color/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:collapsedTitleGravity="left"
                app:expandedTitleMarginTop="185dp"
                app:expandedTitleGravity="left"
                app:expandedTitleMarginStart="30dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/dp_300"
                        android:gravity="center"
                        android:scaleType="fitXY"
                        app:layout_scrollFlags="scroll"
                        android:src="@mipmap/icon_default_user"/>
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_margin="@dimen/dp_10">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="阿伟哈皮"
                            android:textColor="@color/white"
                            android:textSize="@dimen/sp_18"
                            android:textStyle="bold"/>
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="bottom">
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="1关注"
                                android:textSize="@dimen/sp_14"
                                android:textColor="@color/white"/>
                            <TextView
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="1被关注"
                                android:layout_marginStart="@dimen/dp_10"
                                android:textSize="@dimen/sp_14"
                                android:textColor="@color/white"/>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical"
                                android:layout_marginStart="@dimen/dp_10">
                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="+2"
                                    android:layout_marginStart="@dimen/dp_20"
                                    android:textColor="@color/red"/>
                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:text="6人气"
                                    android:textSize="@dimen/sp_14"
                                    android:textColor="@color/white"/>
                            </LinearLayout>
                        </LinearLayout>
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="21/170/60"
                            android:textSize="@dimen/sp_14"
                            android:textColor="@color/light_gray"
                            android:layout_marginTop="@dimen/dp_10"
                            android:drawablePadding="@dimen/dp_10"
                            android:drawableLeft="@mipmap/icon_white_user"/>
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="北京"
                            android:textSize="@dimen/sp_14"
                            android:textColor="@color/light_gray"
                            android:layout_marginTop="@dimen/dp_10"
                            android:drawablePadding="@dimen/dp_10"
                            android:drawableLeft="@mipmap/icon_white_location"/>
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="你还没有填写个人介绍,点击添加"
                            android:textSize="@dimen/sp_14"
                            android:textColor="@color/light_gray"
                            android:layout_marginTop="@dimen/dp_10"
                            android:drawablePadding="@dimen/dp_10" />
                    </LinearLayout>

                </LinearLayout>
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="@dimen/dp_40"
                    app:layout_collapseMode="pin">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="@dimen/dp_30"
                        android:gravity="right">
                        <ImageView
                            android:id="@+id/img_right"
                            android:layout_width="@dimen/dp_24"
                            android:layout_height="@dimen/dp_32"
                            android:layout_gravity="right"
                            android:src="@mipmap/icon_list_setting" />
                    </LinearLayout>

                </androidx.appcompat.widget.Toolbar>


            </com.google.android.material.appbar.CollapsingToolbarLayout>

            <com.flyco.tablayout.CommonTabLayout
                android:id="@+id/tablayout"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dp_40"
                app:tl_iconVisible="false"
                app:tl_textBold="SELECT"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:tl_indicator_width="@dimen/dp_70"
                app:tl_indicator_color="@color/white"
                app:tl_indicator_height="@dimen/dp_2"
                app:tl_textsize="@dimen/sp_16"
                app:tl_textSelectColor="@color/white"
                app:tl_textUnselectColor="@color/white">
            </com.flyco.tablayout.CommonTabLayout>
        </com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>

package com.example.gw.xlp.feature.mine.fragment;

import com.example.gw.xlp.R;
import com.example.gw.xlp.component.widget.TabEntity;
import com.example.gw.xlp.component.widget.base.BaseFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;

public class MyPageFragment extends BaseFragment {


    MyPagerAdapter mAdapter;
    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {
            "动态", "私密", "喜欢"
    };
    @BindView(R.id.tablayout)
    CommonTabLayout mTl3;
    @BindView(R.id.vp)
    ViewPager mVp;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    public static MyPageFragment getInstance() {
        MyPageFragment sf = new MyPageFragment();
        return sf;
    }


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_my;
    }


    @Override
    protected void initViewAndData() {
        mFragments.add(new DynamicFragment());
        mFragments.add(new DynamicFragment());
        mFragments.add(new DynamicFragment());
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], 0, 0));
        }
        mTl3.setTabData(mTabEntities);
        mTl3.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mVp.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        mVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                mTl3.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mVp.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        mVp.setCurrentItem(0);
    }


    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }
}

  • 写回答

3条回答 默认 最新

  • 雲枭 2021-07-14 17:22
    关注

    /**
    * 沉浸式状态栏
    * */
    public static void fullScreen(Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    //5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色
    Window window = activity.getWindow();
    View decorView = window.getDecorView();
    //两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
    int option =
    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
    decorView.setSystemUiVisibility(option);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.TRANSPARENT);
    //导航栏颜色也可以正常设置
    // window.setNavigationBarColor(Color.TRANSPARENT);
    } else {
    Window window = activity.getWindow();
    WindowManager.LayoutParams attributes = window.getAttributes();
    int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    // int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
    attributes.flags |= flagTranslucentStatus;
    // attributes.flags |= flagTranslucentNavigation;
    window.setAttributes(attributes);
    }
    }
    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 7月14日
  • 创建了问题 7月12日

悬赏问题

  • ¥20 Apache poi使用问题
  • ¥21 matlab怎么求时域信号的二阶导数
  • ¥15 判断两个表是否完全相同
  • ¥15 java map类型数据格式,如何快速通过前缀匹配元素
  • ¥15 stc12c5a60s2、QMC5883L、LCD1602组合测量磁场所需程序
  • ¥20 Win11测试yolov4,“找不到nvcuda.dll”怎么办?
  • ¥15 simulink绘制bode图
  • ¥15 php_network_getaddresses: getaddrinfo failed: Name or service not known
  • ¥15 用msg发消息出现的问题
  • ¥15 unity3d机械臂