自定义ToolBar沉浸式适配

1.自定义ImmersiveBar继承ToolBar (v7)

public class ImmersiveBar extends Toolbar {

    private ImageView navigation;
    private TextView title;
    private ImageView img;

    public ImmersiveBar(Context context) {
        this(context, null);
    }

    public ImmersiveBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImmersiveBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.toolbar, this);
        navigation = view.findViewById(R.id.toolbar_navigation);
        title = view.findViewById(R.id.toolbar_title);
        img = view.findViewById(R.id.toolbar_img);
    }

    public void setTitleTextColor(int colorRes) {
        title.setVisibility(View.VISIBLE);
        title.setTextColor(colorRes);
        if (navigation.getVisibility() == View.VISIBLE) {
            navigation.setColorFilter(colorRes, PorterDuff.Mode.SRC_ATOP);
        }
        if (img.getVisibility() == View.VISIBLE) {
            img.setColorFilter(colorRes, PorterDuff.Mode.SRC_ATOP);
        }
        invalidate();
    }

    public void setTitleText(int stringRes) {
        title.setVisibility(View.VISIBLE);
        title.setText(stringRes);
        invalidate();
    }

    public void setNavigationImg(int ImgRes) {
        navigation.setVisibility(View.VISIBLE);
        navigation.setBackgroundResource(ImgRes);
        invalidate();
    }

    public void setNavigationClickListener(OnClickListener listener) {
        navigation.setOnClickListener(listener);
    }

    public void setRightImg(int ImgRes) {
        img.setVisibility(View.VISIBLE);
        img.setBackgroundResource(ImgRes);
        invalidate();
    }

    public void setRightImgClickListener(OnClickListener listener) {
        img.setOnClickListener(listener);
    }
}

布局文件 toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/colorPrimary">

    <ImageView
        android:id="@+id/toolbar_navigation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp"
        android:background="?selectableItemBackground"
        android:clickable="true"
        android:visibility="gone" />

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="64dp"
        android:layout_marginRight="64dp"
        android:ellipsize="end"
        android:gravity="center"
        android:singleLine="true"
        android:textColor="@android:color/black"
        android:textSize="18dp"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/toolbar_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:background="?selectableItemBackground"
        android:clickable="true"
        android:visibility="gone" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@android:color/darker_gray" />
</RelativeLayout>

2.创建布局 base_toolbar.xml 添加自定义ToolBar

<?xml version="1.0" encoding="utf-8"?>
<com.wdx.statusbardemo.ImmersiveBar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/base_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:minHeight="56dp"
    android:paddingTop="25dp" />

3.在Activity中引入布局 base_toolbar.xml

<include layout="@layout/base_toolbar" />

4.在BaseActivity中对ImmersiveBar进行适配

private void initToolbar() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ImmersiveBar toolBar = (ImmersiveBar) findViewById(R.id.base_toolbar);
        if (toolBar != null) {
            int statusBarHeight = -1;
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            }
            int height = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    56, getResources().getDisplayMetrics()));
            int padding_top = ((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                    10, getResources().getDisplayMetrics()));
            int miniHeight = height - statusBarHeight;
            if (Build.VERSION.SDK_INT < 21) {
                LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)
                        toolBar.getLayoutParams();
                linearParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                linearParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
                toolBar.setLayoutParams(linearParams);
                toolBar.setMinimumHeight(miniHeight);
                toolBar.setPadding(0, padding_top, 0, 0);
            }
            setToolBar(toolBar); //回调方法
        }
    }

5.总结

重点在于根据版本设置状态栏透明与paddingTop的值。

阅读更多
版权声明:本文为博主原创文章,转载请注明原文地址。 https://blog.csdn.net/qingjiao233/article/details/78814268
个人分类: Android开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭