Android 自定义View汇总,Demo

Demo

View

ViewGroup

知识

项目

重点内容

标题

沉浸式状态栏
关于状态栏的说法
有说法称这种效果为浸式状态栏,也有说法称之为透明式状态栏。到底是哪种呢?

引用其他文章的一段话:

沉浸式全屏模式隐藏status bar(状态栏)使屏幕全屏,让Activity接收所有的(整个屏幕的)触摸事件。

透明化系统状态栏透明化系统状态栏,使得布局侵入系统栏的后面。
所以个人更觉得是透明化状态栏,因为状态栏还在,下面开始进行实践操作,至于具体叫什么,可以在具体研究,先来实现效果才是首要的。

需要了解
首先要了解到,这种透明式状态栏只有在4.4版本以上才支持,而4.4和5.0版本以上效果还有区别,4.4以下版本默认就是黑色,而4.4版本会有一个渐变阴影的效果,5.0及5.0以上版本则为纯色。

动手实践
1.首先去除默认的Toolbar(标题栏)

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
   </style>

2.由于在实际的项目开发中必然会有一个BaseActivity,所以将控制状态栏的代码封装在父类里,这样每个子类的Activity就拥有了同样的功能。

a、编写BaseActivity的布局activity_base.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/root_view"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <View
        android:id="@+id/status_bar"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"/>
    <FrameLayout
        android:id="@+id/content_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
    </FrameLayout>
</LinearLayout>

由布局可以看出,基类的布局由两部分组成,一个是状态栏View(通过设置背景色达到状态栏的效果),一个是存放Activity内容的View。由于各种手机厂商的手机状态栏高低不一,所以需要在代码中动态获取状态栏的高低。存放Activity内容的View也需要在基类中动态的子类的布局添加到FrameLayout中,这样就能保证每个页面上方都有状态栏。

b、编写BaseActivity中的代码:
首先编写一个ScreenUtil工具类,写一个获取状态栏高度的方法:

/**
     * 获取手机状态栏高度
     *
     * @param context
     * @return
     */
    public static int getStatusBarHeight(Context context) {
        Class<?> c;
        Object obj;
        Field field;
        int x, statusBarHeight = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            statusBarHeight = context.getResources().getDimensionPixelSize(x);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return statusBarHeight;
    }

接下来封装BaseActivity中的方法:

public abstract class BaseActivity extends AppCompatActivity {

    private ViewGroup mContentView;
    private View mStatusView, mLayoutView;
    private boolean mIsLight;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initWindowParameter();
        setContentView(R.layout.activity_base);
        initContentView();
    }

    private void initContentView() {
        mContentView = findViewByID(R.id.content_view);
        mStatusView = findViewByID(R.id.status_bar);
        if (mLayoutView == null) {
            mLayoutView = getLayoutInflater().inflate(getLayoutId(), null);
        }
        mContentView.removeAllViews();
        mContentView.addView(mLayoutView);
        setStatusBarColor(R.color.colorPrimary);
    }

    public abstract int getLayoutId();

    public <T extends View> T findViewByID(int id) {
        return (T) super.findViewById(id);
    }

    /**
     * 隐藏Toolbar
     */
    public void hideStatusBar() {
        mStatusView.setVisibility(View.GONE);
    }

    /**
     * 设置是否为亮色主题
     *
     * @param isLight
     */
    public void setLightStatusBar(boolean isLight) {
        this.mIsLight = isLight;
    }

    /**
     * 设置状态栏颜色
     *
     * @param color
     */
    public void setStatusBarColor(int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LinearLayout
                    .LayoutParams.MATCH_PARENT, ScreenUtil.getStatusBarHeight(this));
            mStatusView.setBackgroundResource(color);
            mStatusView.setLayoutParams(lParams);
            mStatusView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 设置状态栏等与Window相关的参数
     */
    protected void initWindowParameter() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0+
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            if (mIsLight) { //如果StatusBar为亮色主题的话,则文字颜色为深色
                option = option | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4~5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS |
                    localLayoutParams.flags);
        }
    }

}

3、子类继承BaseActivity

public class MainActivity extends BaseActivity {

    private Toolbar mToolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
//        setLightStatusBar(true);  //需要在父类的onCreate方法前执行
        super.onCreate(savedInstanceState);
        mToolbar = findViewByID(R.id.toolbar);
        mToolbar.setTitle(getString(R.string.app_name));

        mToolbar.setVisibility(View.GONE);
        hideStatusBar();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.shawpoo.stutasbar.MainActivity">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/colorPrimary">
    </android.support.v7.widget.Toolbar>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@mipmap/image"/>
</LinearLayout>

由于在开发过程中难免遇到图片为浅色的情况,可以使用View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR,也就是上面代码中的setLightStatusBar()方法。但是只在5.0或5.0以上版本支持。下面是两种颜色的对比图:

如果上面的效果不能满足的话(状态栏信息几乎看不到),可以给状态栏设置一个半透明的阴影,这样会更好些。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值