Android 抽屉侧滑风格+沉浸式状态栏(类似网易云音乐状态栏为纯色)
一.抽屉风格侧滑 Toolbar+DrawerLayout+NavigationView
在andorid studio中new -Module-Navigation Drawer Activity -next创建一个侧滑风格的界面。
在这里只贴出MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private int mStatusBarColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
setStatusBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(false);//禁止默认图标
toggle.setHomeAsUpIndicator(R.mipmap.ic_launcher);//自定义图标
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
//绑定点击事件 这里可以优化代码。
public void onClick(View v) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.openDrawer(GravityCompat.START);
}
});
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
protected void setStatusBar() {
mStatusBarColor = getResources().getColor(R.color.colorPrimary);
StatusBarUtil.setColorNoTranslucentForDrawerLayout(this,(DrawerLayout) findViewById(R.id.drawer_layout),mStatusBarColor);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
// Handle the camera action
} else if (id == R.id.nav_gallery) {
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
} else if (id == R.id.nav_slideshow) {
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
} else if (id == R.id.nav_manage) {
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
} else if (id == R.id.nav_share) {
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
} else if (id == R.id.nav_send) {
Intent intent = new Intent(this,Main2Activity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
在这里将Toolbar中的标题居中的方法
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="dada"
app:collapseIcon="@mipmap/ic_launcher"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="我的页面"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
//并将toolbar.setTitle("")这里设置为空
在StatusBarUtil 状态栏工具类(实现沉浸式状态栏/变色状态栏)发现一个状态栏的工具类。这里进行拆分
在values文件夹中创建一个ids.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="statusbarutil_fake_status_bar_view" />
<item type="id" name="statusbarutil_translucent_view" />
</resources>
写一个工具类,这里直接用拆分出来的代码。
public class StatusBarUtil {
public static