虽然ActionBar已经有一些过时,现在是ToolBar的天下了,但是觉得ToolBar还是不太会用,以后有空再去看ToolBar
俗话说:最好还是google官方原文档。。(其实是我自己说的)
ActionBar是从Android 3.0(API level 11),使用Theme.Holo theme,其最小的SDK版本是11,即:
<manifest ... >
<uses-sdk android:minSdkVersion="11" ... />
...
</manifest>
为了向下兼容,google发布了 v7 appcompat库,作为新时代人类我们当然用Android Studio 省去引入V7包的过程。
1.让我们的Activity继承ActionBarActivity
public class MainActivity extends ActionBarActivity { ... }
2.修改主题
<activity android:theme="@style/Theme.AppCompat.Light" ... >
3.可以修改兼容到Android2.1
<manifest ... >
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="18" />
...
</manifest>
4.在 res/menu/添加menu文件,如:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
5.放置menu条目到actionBar,包括其回调
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
6.为低版本添加返回按钮、在清单文件中配置即可
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
也可以是用app图标作为返回button
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaymessage);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// 如果最低版本高于11使用如下
// getActionBar().setDisplayHomeAsUpEnabled(true);
}
7.给ActionBar添加Tab标签
创建文件res/drawable/actionbar_tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- STATES WHEN BUTTON IS NOT PRESSED -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_selected" />
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tab_unselected_focused" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_selected_focused" />
<!-- STATES WHEN BUTTON IS PRESSED -->
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="true"
android:drawable="@drawable/tab_unselected_pressed" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="true"
android:drawable="@drawable/tab_selected_pressed" />
<!-- Focused states (such as when focused with a d-pad or mouse hover) -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="true"
android:drawable="@drawable/tab_unselected_pressed" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="true"
android:drawable="@drawable/tab_selected_pressed" />
</selector>
在创建文件res/values/themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@style/Theme.AppCompat">
<item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>
<!-- Support library compatibility -->
<item name="actionBarTabStyle">@style/MyActionBarTabs</item>
</style>
<!-- ActionBar tabs styles -->
<style name="MyActionBarTabs"
parent="@style/Widget.AppCompat.ActionBar.TabView">
<!-- tab indicator -->
<item name="android:background">@drawable/actionbar_tab_indicator</item>
<!-- Support library compatibility -->
<item name="background">@drawable/actionbar_tab_indicator</item>
</style>
</resources>
修改MainActivity主题样式为CustomActionBarTheme
最后代码中调用、
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab tab1 = actionBar.newTab();
tab1.setText("标签一").setTabListener(new MyTabListner());
actionBar.addTab(tab1);
ActionBar.Tab tab2 = actionBar.newTab();
tab2.setText("标签二").setTabListener(new MyTabListner());
actionBar.addTab(tab2);
ActionBar.Tab tab3 = actionBar.newTab();
tab3.setText("标签三").setTabListener(new MyTabListner());
actionBar.addTab(tab3);
private class MyTabListner implements ActionBar.TabListener{
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
8.给ActionBar添加抽屉,使用DrawLayout
布局文件:(注意DrawLayout为android.support.v4.widget.DrawerLayout)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/bg_tab"
>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
在MainActivity中
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
//这里我看源码第三个参数确实为drawer indicator,但是并没有修改成功,还是原始的一个箭头,待解决:TODO:
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer_am, R.string.open_drawer, R.string.close_drawer);
drawerLayout.setDrawerListener(drawerToggle);
// 让开关和actionbar建立关系
drawerToggle.syncState();
点击事件自己出来修改return
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show();
return true;
default:
return drawerToggle.onOptionsItemSelected(item) | super.onOptionsItemSelected(item);
}
}
9.Tab标签与ViewPager的绑定,其实就是分别对其设置select监听事件
//给ViewPager设置适配器
mViewPager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
private class MainPagerAdapter extends FragmentStatePagerAdapter {
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//Fagment的List集合
}
@Override
public int getCount() {
return 3;
}
//给ViewPager和tab设置监听事件进行绑定
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
getSupportActionBar().setSelectedNavigationItem(position);
}
});
/**
* ActionBar标签的监听事件
*/
private class MyTabListner implements ActionBar.TabListener {
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
//给ViewPager和tab设置监听事件进行绑定
mViewPager.setCurrentItem(tab.getPosition(), false);
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
遇到的一些问题:
actionBar中不显示app图标:
添加:
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);