标签:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:theme="@style/Theme.ToolBar">
</android.support.v7.widget.Toolbar>
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.menu);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
toolbar.setTitle("Title");//设置主标题
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));//设置主标题颜色
toolbar.setTitleTextAppearance(this, R.style.Theme_ToolBar_Base_Title);//修改主标题的外观,包括文字颜色,文字大小等
<style name="Theme.ToolBar.Base.Title" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">20sp</item>
<item name="android:textColor">@color/color_red</item>
</style>
toolbar.setSubtitle("Subtitle");//设置子标题
toolbar.setSubtitleTextColor(getResources().getColor(android.R.color.darker_gray));//设置子标题颜色
toolbar.setSubtitleTextAppearance(this, R.style.Theme_ToolBar_Base_Subtitle);//设置子标题的外观,包括文字颜色,文字大小等
<style name="Theme.ToolBar.Base.Subtitle" parent="@style/TextAppearance.Widget.AppCompat.Toolbar.Subtitle">
<item name="android:textSize">15sp</item>
<item name="android:textColor">@color/color_43d28d</item>
</style>
toolbar.setNavigationIcon(R.mipmap.home);
toolbar.setLogo(R.mipmap.ic_logo);
android:background="@color/blue"
toolbar.setBackgroundColor(getResources().getColor(R.color.color_red));
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int menuItemId = item.getItemId();
if (menuItemId == R.id.action_search) {
//do something
}
return true;
}
});
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void setMenuAlwaysShow() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
} catch (Exception e) {
e.printStackTrace();
}
}
<style name="Theme.ToolBar.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textSize">20sp</item>
<item name="android:textColorPrimary">@color/black_424242</item>
<!--s设置Menu菜单的背景色-->
<item name="android:itemBackground">@color/blue_600</item>
<!--设置menu菜单不遮挡actionbar-->
<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
<!--配置Menu的图标-->
<item name="actionOverflowButtonStyle">@style/ToolBar.ActionButton.Overflow</item>
</style>
<style name="ToolBar.ActionButton.Overflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">@mipmap/ic_menu_more_overflow</item>
</style>
<style name="Theme.ToolBar.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textSize">20sp</item>
<item name="android:textColorPrimary">@color/color_red</item>
</style>
app:popupTheme="@style/AppTheme.PopupOverlay"
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">>
<item name="android:textColorPrimary">#fff</item>
</style>
<!--s设置Menu菜单的背景色-->
<item name="android:itemBackground">@color/black_light</item>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light">
<!--Item的宽度-->
<item name="android:width">400dp</item>
<item name="android:textColorPrimary">#fff</item>
</style>
<!--<item name="android:drawableLeft">@drawable/ic_action_search</item>-->
<item name="android:drawableRight">@drawable/ic_home_gray_36dp</item>
<item name="android:drawableTop">@drawable/ic_textsms_gray_36dp</item>
@Override
public boolean onCreatePanelMenu(int featureId, Menu menu) {
if (menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return super.onCreatePanelMenu(featureId, menu);
}
<item name="android:dividerHeight">1dp</item>
<style name="Theme.ToolBar.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textSize">20sp</item>
<item name="android:textColorPrimary">@color/color_red</item>
<!--s设置Menu菜单的背景色-->
<item name="android:itemBackground">@color/gray</item>
<!--设置menu菜单不遮挡actionbar-->
<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
</style>
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!--兼容Api 21之前的版本 -->
<item name="overlapAnchor">false</item>
<!-- Api 21-->
<item name="android:overlapAnchor">false</item>
</style>
app:contentInsetStart="3dp"
<style name="Theme.MyActivity" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<!--配置是否使用半透明状态栏-->
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<!--状态栏颜色-->
<item name="android:statusBarColor">@color/color_0176da</item>
</style>
@Override
public boolean onTouchEvent(MotionEvent event) {
if (actionBar == null) {
actionBar = getSupportActionBar();
}
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (actionBar != null) {
if (actionBar.isShowing()) {
actionBar.hide();
} else {
actionBar.show();
}
}
break;
}
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.base_toolbar_menu, menu);
return true;
}
<style name="ToolbarActivityTheme" parent="AppTheme">
<!--启用ActionBar的叠加模式(不生效)-->
<item name="windowActionBarOverlay">true</item>
<!--状态栏半透明-->
<item name="android:windowTranslucentStatus">true</item>
<!--内容填充到导航栏-->
<item name="android:windowTranslucentNavigation">true</item>
<!--设置全屏-->
<item name="android:windowFullscreen">true</item>
</style>
@Override
public boolean onTouchEvent(MotionEvent event) {
if (actionBar == null) {
actionBar = getSupportActionBar();
}
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (actionBar != null) {
if (actionBar.isShowing()) {
int height = toolbar.getMeasuredHeight();
height = height <= 0 ? 112 : height;
startAnimation(-height, new Runnable() {
@Override
public void run() {
actionBar.hide();
}
});
} else {
actionBar.show();
startAnimation(0, new Runnable() {
@Override
public void run() {
}
});
}
}
break;
}
return super.onTouchEvent(event);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void startAnimation(int transY, final Runnable end) {
ViewPropertyAnimator animator = toolbar.animate().translationY(transY).setDuration(600);
animator.start();
animator.setListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
end.run();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
<item name="android:windowTranslucentNavigation">true</item>
<style name="ToolBarActivityTheme" parent="Theme.AppCompat.Light">
<!--使用toolbar取消默认的actionbar-->
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:actionBarStyle">@style/TranslucentActionBar</item>
<!--设置ActionBar的主题-->
<!--<item name="android:toolbarStyle">@style/ToolBarActivityToolbarTheme</item>-->
<item name="toolbarStyle">@style/ToolBarActivityToolbarTheme</item>
</style>
标签:
原文地址:http://blog.csdn.net/c12366456/article/details/51534816