标签:
java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// 如果你的minSdkVersion属性是11或更高, 应该这么用:
// getActionBar().setDisplayHomeAsUpEnabled(true);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
ToastUtils.show(this, "home");
Intent upIntent = NavUtils.getParentActivityIntent(this);
//判断当前Activity在向上导航到目标Intent upIntent是否需要重建新的任务栈,
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
//重建新的任务栈
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(upIntent)
.startActivities();
} else {
//使用当前任务栈
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//清空任务栈中目标Activity实例之上的所有实例
NavUtils.navigateUpTo(this, upIntent);//直接在同一个任务栈跳转
}
return true;//消费掉事件
}
return super.onOptionsItemSelected(item);
}
/*设置左上角Logo Icon*/
actionBar.setLogo(R.drawable.go_back_64px);//(单独设置没作用)
actionBar.setDisplayShowHomeEnabled(true);//是否显示Logo,必须为他setLogo()才起作用
actionBar.setDisplayUseLogoEnabled(true);//是否使用Activity的Logo,即setLogo()方法设置的Logo
actionBar.setDisplayHomeAsUpEnabled(false);//是否显示左上角默认的返回按钮
actionBar.setHomeButtonEnabled(false);//按钮是否可以点击(实测无用,false下依然可以点击--已经设置了该Activity的父级Activity)
<!--设置标题的颜色,注意使用兼容包下的属性-->
<item name="android:titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<item name="titleTextStyle">@style/MyTheme.ActionBar.TitleTextStyle</item>
<style name="MyTheme.ActionBar.TitleTextStyle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">@color/blue</item>
<item name="android:textSize">16sp</item>
</style>
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("debug", "onTouchEvent");
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);
}
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowActionBarOverlay">true</item>
<!-- 兼容支持库 -->
<item name="windowActionBarOverlay">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">false</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<!--设置menu菜单不遮挡actionbar-->
<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
<!--兼容Api 21之前的版本 -->
<item name="overlapAnchor">true</item>
<!-- Api 21-->
<!--<item name="android:overlapAnchor">false</item>-->
</style>
<!--设置menu的文字颜色-->
<!--<item name="actionMenuTextColor">@color/yellow</item>-->
<!--<item name="android:actionMenuTextColor">@color/yellow</item>-->
<!--上面两个设置是无效的-->
<item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>
<style name="myCustomMenuTextApearance" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
<!--文字颜色-->
<item name="android:textColor">@color/blue</item>
<!--文字大小-->
<item name="android:textSize">16sp</item>
</style>
<!--s设置Menu菜单的背景色-->
<item name="android:itemBackground">@color/black_light</item>
actionViewClass: The class of a widget that implements the action.
actionLayout: A layout resource describing the action‘s components.
<item android:id="@+id/action_search"
android:title="@string/action_search"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
@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);
//找到ActionBar上所添加的UI组件的方法:
mSearchView = (SearchView) menu.findItem(R.id.searchItem).getActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String string) {
Toast.makeText(ShowImageActivity.this, "查询:" + string, Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onQueryTextChange(String string) {
return true;
}
});
}
<item
android:id="@+id/custom_search"
android:icon="@drawable/ic_action_search"
android:title="custom_search"
app:actionLayout="@layout/search_layout"
app:showAsAction="collapseActionView|always|withText" />
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/ic_action_search" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
MenuItem collapseActionView = menu.findItem(R.id.searchItem);
// Define the listener
MenuItemCompat.OnActionExpandListener expandListener = new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when action item collapses
ToastUtils.show(ShowImageActivity.this, "action item collapses");
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
ToastUtils.show(ShowImageActivity.this, "action item expanded");
return true; // Return true to expand action view
}
};
MenuItemCompat.setOnActionExpandListener(collapseActionView, expandListener);
标签:
原文地址:http://blog.csdn.net/c12366456/article/details/51420512