其他分享
首页 > 其他分享> > Android ActionBar

Android ActionBar

作者:互联网

android的ActionBar是3.0才推出的,3.0之前称之为AppBar。为了向后兼容,ActionBar位于Android的支持库AppCompat中,所以要使用ActionBar先必须依赖AppCompat库(现在新建的工程默认都依赖此库了)

implementation 'androidx.appcompat:appcompat:1.3.0'

如果没有在主题Theme中或Activity的代码中明确指定NoActionBar,那么ActionBar都是默认存在的,要获取ActionBar对象使用

ActionBar ationBar = getSupportActionBar();

ActionBar就四个东西
image

左一 一般联动DrawerLayout使用

    private DrawerLayout dl;
    private ActionBarDrawerToggle toggle;
        toggle = new ActionBarDrawerToggle(this,dl,R.string.open,R.string.close);
        dl.addDrawerListener(toggle);
        toggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (toggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

左二一般显示图标和标题

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.mipmap.ic_launcher_round);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
		...

右一右二使用menu菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/main_search"
        android:icon="@android:drawable/ic_menu_search"
        android:title="search"
        app:showAsAction="always" />
</menu>
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.main_search){

             return true;
        }
        return super.onOptionsItemSelected(item);
    }

标签:ActionBar,return,menu,item,toggle,Android,true
来源: https://www.cnblogs.com/maowuge/p/16688877.html