Android 抽屉侧滑风格+透明状态栏(纯色)

本文介绍了如何在Android应用中实现抽屉式侧滑菜单,并结合沉浸式状态栏,使其呈现出类似网易云音乐的纯色状态栏效果。首先通过Android Studio创建Navigation Drawer Activity,然后调整Toolbar使标题居中。接着利用StatusBarUtil工具类实现状态栏颜色的设置,创建ids.xml文件,并编写工具类。最后在初始化后调用相应方法,设置状态栏为纯色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值