使用Toolbar + DrawerLayout实现侧滑和改变toolbar左边按钮颜色

1.首先是xml文件:

ac_main.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    layout="@layout/app_title" />

    layout="@layout/test_main_content" />


app_title.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

            android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

                    android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?attr/colorPrimary"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
        
                    android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="标题"
            android:textColor="@color/White"
            android:textSize="@dimen/size_18" />

    

test_main_content.xml

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

            android:id="@+id/iv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/background1" />

            android:id="@+id/lv_left_menu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:src="@mipmap/background2" />


下面是Java代码

public class MainActivity extends BaseActivity {

    private Toolbar toolbar;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    private ImageView ivRunningMan, iv_left;
    private AnimationDrawable mAnimationDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_main);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ivRunningMan = findViewId(R.id.iv_main);
        iv_left = findViewId(R.id.lv_left_menu);
        mDrawerLayout = findViewId(R.id.dl_left);
        ivRunningMan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("1111111");
            }
        });
        iv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toast("2222222222222");
            }
        });
        initView();
    }

    void initView() {

        toolbar.setTitle("Toolbar");//设置Toolbar标题
        toolbar.setTitleTextColor(Color.parseColor("#ffffff")); //设置标题颜色
        toolbar.setNavigationIcon(R.mipmap.icon_back);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }
        };
        mDrawerToggle.syncState();
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    }

样式文件






    android:name=".MainActivity"
    android:theme="@style/ChangeToolbarLeftColor" />


里面有的资源文件自己随便改就可以了,以上代码本人亲人测试过,么问题;下面是个效果图片,页面比较丑,勿喷,只为测试,





使用Toolbar + DrawerLayout实现侧滑和改变toolbar左边按钮颜色_第1张图片












你可能感兴趣的:(Android,toolbar,toolbar侧滑,侧滑,toolbar左边按钮颜色改变)