状态栏的透明效果实现

技术的提高是因为有需求,产品提出来了,程序员们就负责实现吧,谁让现在流行跟风呢,嘿嘿。其实也是从网上查看的资料,自己总结一下,以便以后方便查询,毕竟自己的代码自己能看懂,嘿嘿。

首先,需要引入一个v7的包:

compile ‘com.android.support:appcompat-v7:22.2.1’

之后创建一个ToolBar的布局文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@mipmap/titlebg"
    android:minHeight="?attr/actionBarSize"
    android:paddingTop="@dimen/toolbar_padding_top"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<!--android:background="@color/colorPrimary"-->此属性就是设置状态栏的,可以设置为图片,也可以设置与应用相同的颜色。

设置好toolbar之后就可以引用这个布局了,其实这个和之前自己设置的布局引入相同,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/tool_bar" />
</LinearLayout >

实现之后使用Activity进行代码实现就可以了。

public class BaseActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private LinearLayout mllBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                mllBar.setFitsSystemWindows(true);
                mllBar.setClipToPadding(false);
            }
        }

        initToolBar();
    }

    @Override
    public void setContentView(int layoutResID) {
        setContentView(View.inflate(this, layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
        mllBar = (LinearLayout) findViewById(R.id.ll_bar);
        if (mllBar == null) return;
        mllBar.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        initToolBar();
    }

    private void initToolBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
    }
}

运行可能会出现错误,需要将其主题的parent修改为:

  <item name="android:windowTranslucentStatus">true</item>

此文章主要查看了stromzhang发表的文章

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值