您的位置:首页 > 其它

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

2017-09-08 15:53 489 查看
1.首先是xml文件:

ac_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

<include layout="@layout/app_title" />

<include layout="@layout/test_main_content" />

</LinearLayout>


app_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

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

<android.support.v7.widget.Toolbar
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" />
<!--app:popupTheme="@style/AppTheme.PopupOverlay"-->
<TextView
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" />

</RelativeLayout>
</LinearLayout>


test_main_content.xml

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

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

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

</android.support.v4.widget.DrawerLayout>


下面是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);
}


样式文件

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowNoTitle">true</item>
<item name="android:windowNoTitle">true</item>
</style>
<!--改变toolbar的左边按钮的颜色-->
<style name="ChangeToolbarLeftColor" parent="AppTheme">
<!--返回键样式-->
<item name="drawerArrowStyle">@style/AppTheme.DrawerArrowToggle</item>
</style>

<style name="AppTheme.DrawerArrowToggle" parent="Base.Widget.AppCompat.DrawerArrowToggle">
<!-- 这个颜色就改变toolbar的按钮的颜色配置,只能改变颜色不能改变图片-->
<item name="color">@android:color/white</item>
</style>


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


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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息