Android Toolbar 沉浸式使用

对于沉浸式的应用,网上有很多,我总结后写成两个基类,可以直接使用,说下注意的点有:1.用toolbar的activity主题要no actionbar;2.想要沉浸式的图片置顶需要Android5。0,这个代码要适配;3.更多的坑自己网上解决,培养下记录解决方法的习惯,最好自己写demo,例如文字居中,文字重叠。补充下:样式下colorPrimaryDark的颜色值是状态栏的,progressdialog圈圈的颜色值可以设置 android:indeterminateTint="@color/color_green",配置更多的统一色调。
本demo的程序截图:
![首页功能](https://img-blog.csdn.net/20180426095008124?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2o3YTJzb24=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![一般toolbar的使用](https://img-blog.csdn.net/20180426095041134?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2o3YTJzb24=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![图片占据状态栏>=5.0](https://img-blog.csdn.net/20180426095111716?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2o3YTJzb24=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![状态栏带阴影](https://img-blog.csdn.net/20180426095200541?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2o3YTJzb24=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![收缩布局的使用-展开状态](https://img-blog.csdn.net/20180426095240941?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2o3YTJzb24=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
![收缩布局-收缩状态](https://img-blog.csdn.net/20180426095307209?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2o3YTJzb24=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)

代码使用:

package jason.com.blankbardemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import jason.com.blankbardemo.ui.CollspingAty;
import jason.com.blankbardemo.ui.ImgTopAty;
import jason.com.blankbardemo.ui.ToolBarAty;
import jason.com.blankbardemo.ui.TransluBarAty;

public class MainActivity extends AppCompatActivity {

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

    public void onClick_1(View view) {
        Intent intent = new Intent(this, ToolBarAty.class);
        startActivity(intent);
    }

    public void onClick_2(View view) {
        Intent intent = new Intent(this, ImgTopAty.class);
        startActivity(intent);

    }

    public void onClick_3(View view) {
        Intent intent = new Intent(this, TransluBarAty.class);
        startActivity(intent);
    }

    public void onClick_4(View view) {
        Intent intent = new Intent(this, CollspingAty.class);
        startActivity(intent);
    }
}

/ 直接继承基类就可以使用到toolbar

package jason.com.blankbardemo.ui;

import android.os.Bundle;

import jason.com.blankbardemo.R;
import jason.com.blankbardemo.base.BasebarAty;

//这个基类中我代码做了写死状态栏的高度=toolbar的padding,导致阴影会被toolbar的背景颜色覆盖掉
//我这么做,是避免在复杂的嵌套布局中留白,有时用fitwindow...=true,也是解决不了,例如CollapsingToolbarLayout
//如果想要阴影就继承baseimgAty就好
public class ToolBarAty extends BasebarAty {

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

    private void initAty() {
        super.setBarTitle("带toolbar");
        super.showToolbar(true);    //默认为true
    }
}

//      true 设置状态栏透明
public class ImgTopAty extends BaseimgAty {

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

/
package jason.com.blankbardemo.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.widget.TextView;

import jason.com.blankbardemo.R;
import jason.com.blankbardemo.base.BaseimgAty;

public class CollspingAty extends BaseimgAty {

private AppBarLayout mAppBarLayout;
private CollapsingToolbarLayout mBarLayout;
private Toolbar mToolbar;
private TextView tv_title;

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

    this.mAppBarLayout = (AppBarLayout) findViewById(R.id.layout_bar);
    this.mBarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_layout);
    this.mToolbar = (Toolbar) findViewById(R.id.toolbar1);
    this.tv_title = (TextView) findViewById(R.id.tv_title);

    this.setSupportActionBar(mToolbar);
    this.mBarLayout.setTitleEnabled(false); //没有动画 否则不会居中
    mBarLayout.setExpandedTitleGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    mBarLayout.setCollapsedTitleTextColor(Color.WHITE); //收起时字体颜色
    mBarLayout.setExpandedTitleColor(Color.WHITE);
    mBarLayout.setExpandedTitleMarginBottom(30);
    tv_title.setText("房屋详情");

    ActionBar actionBar = getSupportActionBar();
    if (null != actionBar) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
    }
    this.mAppBarLayout.setBackgroundResource(R.drawable.bgpic);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
    }
    return true;
}

}

//大家的Androidstudio版本可能都不同,这里下载解压后不要着急打开项目,打开项目文件夹修改几个文件可以使用你本地的gradle版本,打开你原来运行过的项目替换以下代码:
1.打开项目的build.gradle文件 ,修改替换成你原有的项目代码语句,保存文件。
//classpath ‘com.android.tools.build:gradle:2.2.2’
2.打开项目gradle/warpper/gradle-wrapper.properties文件,替换保存:
//distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip
3.打开项目app/build.gradle,修改以下工具版本,打开你原来运行过的项目替换以下代码:
//compileSdkVersion 25
//buildToolsVersion “25.0.3”
//compile ‘com.android.support:appcompat-v7:25.3.1’
//compile ‘com.android.support:design:25.3.1’

本文demo代码下载点这里

你可能感兴趣的:(Android Toolbar 沉浸式使用)