本文主要是介绍Toolbar简单封装,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.修改style.xml
<resources><style name="AppTheme.Base"parent="Theme.AppCompat.Light.NoActionBar"><item name="windowActionBar">false</item><item name="android:windowNoTitle">true</item><item name="windowNoTitle">true</item></style><style name="AppTheme" parent="AppTheme.Base"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item><!-- toolbar --><!-- menu的字体颜色(记住不要android:) --><item name="actionMenuTextColor">@android:color/white</item><item name="android:textSize">18sp</item></style>
</resources>
2.编写ToolBarActivity(标题居中)和ToolBarLeftTitleActivity(标题居左)
package com.zx.toolbar;import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;public class ToolBarActivity extends AppCompatActivity {public static final String LOG_TAG = ToolBarActivity.class.getSimpleName();public static final int TITLE_MODE_LEFT = -1;public static final int TITLE_MODE_CENTER = -2;public static final int TITLE_MODE_NONE = -3;private int titleMode = TITLE_MODE_CENTER;protected TextView tvTitle;protected Toolbar toolbar;protected FrameLayout toolbarLayout;private LinearLayout contentView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initToolBar();initContentView();}private void initToolBar() {toolbarLayout = new FrameLayout(this);LayoutInflater.from(this).inflate(R.layout.layout_toobar, toolbarLayout,true);tvTitle = (TextView) toolbarLayout.findViewById(R.id.tv_title);toolbar = (Toolbar) toolbarLayout.findViewById(R.id.toolbar);toolbar.setTitleTextColor(getResources().getColor(R.color.primary_text_default_material_dark));setSupportActionBar(toolbar);ActionBar actionBar = getSupportActionBar();if (actionBar != null) {actionBar.setDisplayShowHomeEnabled(true);actionBar.setDisplayHomeAsUpEnabled(true);actionBar.setDisplayUseLogoEnabled(false);actionBar.setHomeAsUpIndicator(R.mipmap.icon_back);}}protected void initContentView() {contentView = new LinearLayout(this);contentView.setOrientation(LinearLayout.VERTICAL);}protected void setToolbarCustomView(int layoutResId) {try {LayoutInflater.from(this).inflate(layoutResId, toolbar, true);} catch (Exception e) {Log.w(LOG_TAG, "set toolbar customview exception", e);}tvTitle.setVisibility(View.GONE);}protected void setToolbarCustomView(View view) {if (toolbarLayout != null && view != null) {toolbar.addView(view, view.getLayoutParams());}tvTitle.setVisibility(View.GONE);}protected View getContentView() {return contentView;}@Overridepublic void setContentView(int layoutResID) {// 添加toolbar布局contentView.addView(toolbarLayout, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));LayoutInflater.from(this).inflate(layoutResID, contentView, true);super.setContentView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));}@Overridepublic void setContentView(View view) {setNewContentView(view, view.getLayoutParams());}@Overridepublic void setContentView(View view, LayoutParams params) {setNewContentView(view, params);}private void setNewContentView(View view, LayoutParams params) {// 添加toolbar布局contentView.addView(toolbarLayout, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));// content FrameLayoutFrameLayout contentLayout = new FrameLayout(this);if (params == null) {params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);}contentLayout.addView(view, params);// 添加content布局contentView.addView(contentLayout, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));super.setContentView(contentView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));}public void setTitle(CharSequence title, int mode) {this.titleMode = mode;setTitle(title);}public void setTitleMode(int mode) {this.titleMode = mode;setTitle(getTitle());}@Overrideprotected void onTitleChanged(CharSequence title, int color) {super.onTitleChanged(title, color);if (titleMode == TITLE_MODE_NONE) {if (toolbar != null) {toolbar.setTitle("");}if (tvTitle != null) {tvTitle.setText("");}} else if (titleMode == TITLE_MODE_LEFT) {if (toolbar != null) {toolbar.setTitle(title);}if (tvTitle != null) {tvTitle.setText("");}} else if (titleMode == TITLE_MODE_CENTER) {if (toolbar != null) {toolbar.setTitle("");}if (tvTitle != null) {tvTitle.setText(title);}}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if (item.getItemId() == android.R.id.home) {back();return true;}return super.onOptionsItemSelected(item);}private void back() {super.onBackPressed();}protected void setNavigationIcon(int resId) {if (toolbar != null) {toolbar.setNavigationIcon(resId);}}protected void setNavigationIcon(Drawable icon) {if (toolbar != null) {toolbar.setNavigationIcon(icon);}}}
测试使用:
package com.zx.toolbar;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;public class MainActivity extends ToolBarActivity {/* 1.继承ToolBarActivity,默认就有返回箭头,标题居中,继承ToolBarLeftTitleActivity标题居左2.把返回箭头换成叉:setNavigationIcon(R.drawable.ic_web_close);去掉返回箭头:setNavigationIcon(null);3.想要改变标题栏中间的字直接在menifest中的<activityandroid:name=".BusinessActivity"android:label="在这里改字" > label 改就行了</activity>4。需要添加头部右边的图标请正常添加menu5.改变右上角menu的字体颜色,在style中添加<item name="actionMenuTextColor">@android:color/white</item>(记住前面不要有android:,不然就拜拜)*/@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
/* 看一看menu.add方法的参数:第一个int类型的group ID参数,代表的是组概念,你可以将几个菜单项归为一组,以便更好的以组的方式管理你的菜单按钮。第二个int类型的item ID参数,代表的是项目编号。这个参数非常重要,一个item ID对应一个menu中的选项。在后面使用菜单的时候,就靠这个item ID来判断你使用的是哪个选项。第三个int类型的order ID参数,代表的是菜单项的显示顺序。默认是0,表示菜单的显示顺序就是按照add的显示顺序来显示。第四个String类型的title参数,表示选项中显示的文字。*/public boolean onCreateOptionsMenu(Menu menu) {//条用基类的方法,以便调出系统菜单(如果有的话)//super.onCreateOptionsMenu(menu);MenuItem item1 = menu.add(0, 1, 1, "实例1");//item1.setIcon(R.drawable.icon_search);item1.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);MenuItem item2 = menu.add(0, 2, 2, "实例2");item2.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);//返回值为“true”,表示菜单可见,即显示菜单return true;}public boolean onOptionsItemSelected(MenuItem item) {if (item.getItemId() == 1) {Toast.makeText(this, "实例1", Toast.LENGTH_SHORT).show();return true;} else if (item.getItemId() == 2) {Toast.makeText(this, "实例2", Toast.LENGTH_SHORT).show();return true;}return super.onOptionsItemSelected(item);}
}
layout_toobar.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><android.support.v7.widget.Toolbar
android:id="@+id/toolbar"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="@color/colorPrimary"android:paddingLeft="0dp"android:paddingRight="8dp"><TextView
android:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/app_name"android:textColor="@android:color/white"android:textSize="18sp"android:textStyle="normal" /></android.support.v7.widget.Toolbar></LinearLayout>
注意Manifest的Theme设置为AppTheme
这篇关于Toolbar简单封装的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!