Android 5.0的时候就用Toolbar替代了ActionBar,而ActionBarActivity又被AppCompatActivity替代,那么使用方法就是引入Toolbar设置到Activity中
一个应用的几乎所有界面都需要头部app bar,为了能够方便各个界面方便使用,统一管理,这里就来对toolBar进行简单的封装。
先上两张图:
package com.hdc.toolbardemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
/**
* Created by wk on 2016/7/7.
* Base Activity.
*/
public abstract class BaseAppCompatActivity extends AppCompatActivity {
private static final String TAG = BaseAppCompatActivity.class.getSimpleName();
private TextView mToolbarTitle;
private TextView mToolbarSubTitle;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
mToolbar = (Toolbar) findViewById(R.id.toolbar);
/*
toolbar.setLogo(R.mipmap.ic_launcher);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub Title");
*/
mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);
mToolbarSubTitle = (TextView) findViewById(R.id.toolbar_subtitle);
if (mToolbar != null) {
//将Toolbar显示到界面
setSupportActionBar(mToolbar);
}
if (mToolbarTitle != null) {
//getTitle()的值是activity的android:lable属性值
mToolbarTitle.setText(getTitle());
//设置默认的标题不显示
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
@Override
protected void onStart() {
super.onStart();
/**
* 判断是否有Toolbar,并默认显示返回按钮
*/
if(null != getToolbar() && isShowBacking()){
showBack();
}
}
/**
* 获取头部标题的TextView
* @return
*/
public TextView getToolbarTitle(){
return mToolbarTitle;
}
/**
* 获取头部标题的TextView
* @return
*/
public TextView getSubTitle(){
return mToolbarSubTitle;
}
/**
* 设置头部标题
* @param title
*/
public void setToolBarTitle(CharSequence title) {
if(mToolbarTitle != null){
mToolbarTitle.setText(title);
}else{
getToolbar().setTitle(title);
setSupportActionBar(getToolbar());
}
}
/**
* this Activity of tool bar.
* 获取头部.
* @return support.v7.widget.Toolbar.
*/
public Toolbar getToolbar() {
return (Toolbar) findViewById(R.id.toolbar);
}
/**
* 版本号小于21的后退按钮图片
*/
private void showBack(){
//setNavigationIcon必须在setSupportActionBar(toolbar);方法后面加入
getToolbar().setNavigationIcon(R.mipmap.icon_back);
getToolbar().setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
/**
* 是否显示后退按钮,默认显示,可在子类重写该方法.
* @return
*/
protected boolean isShowBacking(){
return true;
}
/**
* this activity layout res
* 设置layout布局,在子类重写该方法.
* @return res layout xml id
*/
protected abstract int getLayoutId();
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy...");
}
}
package com.hdc.toolbardemo;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends BaseAppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getToolbarTitle().setText("主界面");
getSubTitle().setText("更多");
}
/**
* 设置不显示返回按钮
*
* @return
*/
protected boolean isShowBacking() {
return false;
}
/**
* 设置布局
*
* @return
*/
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
/**
* 跳转到登录界面
*
* @param view
*/
public void login(View view) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.menu_about:
Toast.makeText(MainActivity.this, ""+"关于", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_settings:
Toast.makeText(MainActivity.this, ""+"设置", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_quit:
Toast.makeText(MainActivity.this, ""+"退出", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
http://www.myexception.cn/android/2053784.html
Android 5.x Theme 与 ToolBar 实战
http://blog.csdn.net/lmj623565791/article/details/45303349