标签:
最新版android-support-v7-appcompat,android-support-v4
AppBarActivity
import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.Locale; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; import com.example.explistview.AnimExpandActivity; import com.example.explistview.AppBarActivity; import com.example.explistview.DrawerActivity; import com.example.explistview.SlidingPaneLayoutActivity; public class MainActivity extends AppBarActivity implements ActionBar.TabListener { ViewPager mViewPager; private SectionsPagerAdapter mSectionsPagerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); forceShowOverflowMenu(); // Set up the action bar. final ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(true); actionBar.setDisplayShowCustomEnabled(true); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setCustomView(R.layout.custom_actionbar); TextView tvTitle = (TextView) actionBar.getCustomView().findViewById(R.id.m_actionbar_title); tvTitle.setText("自定义标题"); mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { actionBar.addTab(actionBar.newTab() .setText(mSectionsPagerAdapter.getPageTitle(i)) .setTabListener(this)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { startActivity(new Intent(this, DrawerToggleActivity.class)); return true; } else if(id==android.R.id.home) { return true; } else if(id==R.id.action_camera) { startActivity(new Intent(this, AnimExpandActivity.class)); return true; } else if(id==R.id.action_mail) { startActivity(new Intent(this, DrawerActivity.class)); return true; } else if(id==R.id.action_plus) { startService(new Intent(MainActivity.this, RemoteService.class)); Log.e("MainActivity", "startService"); startActivity(new Intent(this, SlidingPaneLayoutActivity.class)); return true; } return super.onOptionsItemSelected(item); } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { // When the given tab is selected, switch to the corresponding page in // the ViewPager. mViewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } private void forceShowOverflowMenu() { try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class .getDeclaredField("sHasPermanentMenuKey"); if (menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception e) { e.printStackTrace(); } } @Override public boolean onMenuOpened(int featureId, Menu menu) { if (featureId == Window.FEATURE_ACTION_BAR && 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) { } } } return super.onMenuOpened(featureId, menu); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment (defined as a static inner class // below). return PlaceholderFragment.newInstance(position + 1); } @Override public int getCount() { // Show 3 total pages. return 3; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(l); case 1: return getString(R.string.title_section2).toUpperCase(l); case 2: return getString(R.string.title_section3).toUpperCase(l); } return null; } } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { /** * The fragment argument representing the section number for this * fragment. */ private static final String ARG_SECTION_NUMBER = "section_number"; /** * Returns a new instance of this fragment for the given section number. */ public static PlaceholderFragment newInstance(int sectionNumber) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); fragment.setArguments(args); return fragment; } public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); TextView textView = (TextView) rootView .findViewById(R.id.section_label); textView.setText(Integer.toString(getArguments().getInt( ARG_SECTION_NUMBER))); return rootView; } } @Override protected void onDestroy() { stopService(new Intent(MainActivity.this, RemoteService.class)); super.onDestroy(); } }
layout
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.actiontabbar.MainActivity" />
theme-styles
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- API 14 theme customizations can go here. --> <item name="actionBarStyle">@style/AppBaseActionStyle</item> <item name="actionOverflowMenuStyle">@style/AppBaseActionOverflowMenuStyle</item> <item name="actionMenuTextAppearance">@style/AppBaseMenuStyle</item> <item name="actionMenuTextColor">@android:color/white</item> <item name="actionDropDownStyle">@style/AppBaseDropDownStyle</item> <item name="actionButtonStyle">@style/AppBaseActionButtonStyle</item> <item name="android:textColor">@android:color/white</item> </style> <style name="AppBaseActionStyle" parent="@style/Widget.AppCompat.Light.ActionBar.Solid" > <item name="background">@color/material_deep_teal_500</item> <item name="backgroundStacked">@color/material_blue_grey_900</item> <item name="backgroundSplit">@android:color/white</item> <item name="height">@dimen/abc_action_button_min_height_material</item> <item name="android:paddingTop">0dip</item> </style> <style name="AppBaseActionOverflowMenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu.Overflow"> <item name="overlapAnchor">false</item> <item name="android:popupBackground">@color/material_deep_teal_500</item> <item name="android:dropDownVerticalOffset">-3dip</item> <item name="android:textColorPrimary">@android:color/white</item> <item name="actionMenuTextColor">@android:color/white</item> </style> <style name="AppBaseMenuStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"> <item name="android:textColor">@android:color/white</item> <item name="android:textSize">16sp</item> <item name="android:textStyle">normal</item> </style> <style name="AppBaseDropDownStyle" parent="@style/Widget.AppCompat.Light.Spinner.DropDown.ActionBar"> <item name="android:textColor">@android:color/white</item> </style> <style name="AppBaseActionButtonStyle" parent="@style/Widget.AppCompat.Light.ActionButton"> <item name="android:background">?attr/actionBarItemBackground</item> <item name="android:paddingLeft">12dip</item> <item name="android:paddingRight">12dip</item> <item name="android:minWidth">@dimen/abc_action_button_min_width_material</item> <item name="android:height">@dimen/abc_action_button_min_height_material</item> <item name="android:gravity">center</item> </style>
注意overlapAnchor的值
import java.lang.reflect.Method; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.support.v7.internal.view.menu.MenuBuilder; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.example.actiontabbar.R; public class AppBarActivity extends ActionBarActivity{ private Toolbar mToolbar; @Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); setContentView(R.layout.common_toolbar); mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setNavigationIcon(R.drawable.ktv_ic_main_hot_pressed); mToolbar.setTitle("Rocko");// 标题的文字需在setSupportActionBar之前,不然会无效 mToolbar.inflateMenu(R.menu.main); /* 菜单的监听可以在toolbar里设置,也可以像ActionBar那样,通过Activity的onOptionsItemSelected回调方法来处理 */ setShortcutsVisible(mToolbar.getMenu()); mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: Toast.makeText(AppBarActivity.this, "action_settings", 0).show(); break; case R.id.action_mail: Toast.makeText(AppBarActivity.this, "action_mail", 0).show(); break; default: break; } return true; } }); mToolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("Navigation", "Click"); } }); } private void setShortcutsVisible(Menu menu) { if(MenuBuilder.class.isInstance(menu)) { MenuBuilder builder = (MenuBuilder) menu; builder.setShortcutsVisible(true); try { Method m = menu.getClass().getDeclaredMethod( "setOptionalIconsVisible", Boolean.TYPE); m.setAccessible(true); m.invoke(builder, true); } catch (Exception ie) { } } } }
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" 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="wrap_content" android:background="@color/material_deep_teal_500" android:minHeight="?attr/actionBarSize" app:popupTheme="@style/AppBarTheme" app:theme="@style/ThemeOverlay.AppCompat.ActionBar" > </android.support.v7.widget.Toolbar> </LinearLayout>
AppBarBaseTheme
<style name="Theme.AppCompat.NoActionBar"> <item name="windowActionBar">false</item> <item name="android:windowNoTitle">true</item> </style>
AppBarTheme
<style name="AppBarTheme" parent="ThemeOverlay.AppCompat.Light"> <item name="actionDropDownStyle">@style/AppBaseToolBarMenuDropDownStyle</item> <item name="actionOverflowMenuStyle">@style/AppBaseToolbarOverflowMenuStyle</item> <item name="android:textColor">@android:color/white</item> </style> <style name="AppBaseToolBarMenuDropDownStyle" parent="@style/Base.Widget.AppCompat.Spinner.DropDown.ActionBar"> <item name="overlapAnchor">false</item> <item name="android:popupBackground">@color/material_deep_teal_500</item> <item name="android:dropDownHorizontalOffset">-4dip</item> </style> <style name="AppBaseToolbarOverflowMenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu.Overflow"> <item name="overlapAnchor">false</item> <item name="android:popupBackground">@color/material_deep_teal_500</item> <item name="android:textColorPrimary">@android:color/white</item> </style>
Android AppBar样式设计?ActionBar/Toolbar
标签:
原文地址:http://my.oschina.net/ososchina/blog/491153