接上篇博文:http://blog.csdn.net/geniuseoe2012/article/details/8982469
其中提到了Github-Client用到了很多開源的第三方庫
首單其沖的便是ActionBarSherklock ,眾所周知從Android3.0開始就提供了ActionBar,這讓Android的使用者體驗強了好多,但是這個ActionBar在Android3.0以下是不支援的,Google也沒推出什麼解決方案,不過沒有什麼是做不到的,在github上有牛人放出了開源的ActionBarSherlock,使得在Android2.x上實現ActionBar,如果你在Android3.0的手機上使用,它就會調用原生的ActionBar,下面藍老師就結合實際用例簡單的談談它的使用
首先從https://github.com/JakeWharton/ActionBarSherlock 上下載源碼,得到目錄
其中actionbarsherlock作為連結工程要被其它工程引用
actionbarsherlock-samples裡是一些樣本工程,其實就是一些API的使用demo
這個狠重要,童鞋們要仔細看,對著代碼把樣本全都跑一遍,基本就能掌握其使用方法了
不過由於demo裡的樣本大多用的ActionBarSherklock 原生樣式或是系統內建樣式
而我們的項目往往有一套自己的風格,這就需要我們改造style設定theme
具體可參照樣本工程style,其效果運行如下:
本文就以網易新聞用戶端的actionbar應用為藍本,來剖析一下各種奧妙
先上,原版
藍老師的山寨版
上程式碼片段:
public class SliderTabPagerActivity extends SherlockFragmentActivity implements OnPageChangeListener,TabListener{private int COUNT = 0;private List<DataStruct> mList;private ViewPager mViewPager;private LayoutInflater mInflater;@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0);setContentView(R.layout.slider_tab_pager_layout); setupViews();initData();} @Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getSupportMenuInflater().inflate(R.menu.main_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Toast.makeText(this, "home", Toast.LENGTH_SHORT).show(); return true; case R.id.menu_setting: Toast.makeText(this, "setting", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } }private void setupViews(){ mInflater = getLayoutInflater(); mViewPager = (ViewPager)findViewById(R.id.pager); mViewPager.setOnPageChangeListener(this); ActionBar actionBar = getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); String[] arrStrings = getResources().getStringArray(R.array.sections); COUNT = arrStrings.length; for (int i = 0; i < COUNT; i++) { Tab tab = actionBar.newTab(); tab.setCustomView(getTabView(arrStrings[i])); tab.setTabListener(this); actionBar.addTab(tab); } actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setLogo(R.drawable.biz_pics_main_back_normal); actionBar.setTitle("網易圖片");}private void initData(){mList = new ArrayList<DataStruct>();for(int i = 0; i < COUNT; i++){DataStruct struct = new DataStruct();struct.index = i;struct.daString = "tab-->" + i;mList.add(struct);}mViewPager.setAdapter(new SliderPagerAdapter(getSupportFragmentManager(), mList));}private View getTabView(String title){View view = mInflater.inflate(R.layout.tab_item_layout, null);TextView textView = (TextView) view.findViewById(R.id.textView);textView.setText(title);return view;}@Overridepublic void onPageScrollStateChanged(int arg0) {// TODO Auto-generated method stub}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {// TODO Auto-generated method stub}@Overridepublic void onPageSelected(int position) { getSupportActionBar().setSelectedNavigationItem(position);}@Overridepublic void onTabSelected(Tab tab, FragmentTransaction ft) { mViewPager.setCurrentItem(tab.getPosition());}@Overridepublic void onTabUnselected(Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub}@Overridepublic void onTabReselected(Tab tab, FragmentTransaction ft) {// TODO Auto-generated method stub}class SliderPagerAdapter extends FragmentStatePagerAdapter{private List<DataStruct> mList;public SliderPagerAdapter(FragmentManager fm, List<DataStruct> list) {super(fm);mList = list;}@Overridepublic Fragment getItem(int pos) {return new SliderFragment(mList.get(pos));}@Overridepublic int getCount() {return mList.size();}}public static class SliderFragment extends SherlockFragment{private DataStruct mStruct;public SliderFragment(DataStruct struct){mStruct = struct;}@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.layout1, null);TextView tView = (TextView) view.findViewById(R.id.textView);tView.setText(mStruct.toString());return view;}}}
Sytle配置
<style name="Themes.Sliders.Actionbar" parent="@style/Theme.Sherlock.Light"> <item name="actionBarStyle">@style/Widget.Slider.ActionBar</item> <item name="actionBarTabStyle">@style/Widget.Slider.ActionBarTab</item> <item name="actionBarItemBackground">@drawable/selector_actionbar_button</item> <item name="homeAsUpIndicator">@drawable/biz_widget_left</item> <item name="android:actionBarStyle">@style/Widget.Slider.ActionBar</item> <item name="android:actionBarTabStyle">@style/Widget.Slider.ActionBarTab</item> <item name="android:actionBarItemBackground">@drawable/selector_actionbar_button</item> <item name="android:homeAsUpIndicator">@drawable/biz_widget_left</item> </style> <style name="Widget.Slider.ActionBar" parent="@style/Widget.Sherlock.ActionBar"> <item name="android:background">@drawable/base_actionbar_bg</item> <item name="android:backgroundStacked">#ffeeeeee</item> <item name="android:titleTextStyle">@style/TextAppearance.Slider.Widget.ActionBar.Titlen</item> <item name="titleTextStyle">@style/TextAppearance.Slider.Widget.ActionBar.Titlen</item> <item name="background">@drawable/base_actionbar_bg</item> <item name="backgroundStacked">#ffeeeeee</item> </style> <style name="Widget.Slider.ActionBarTab" parent="@style/Widget.Sherlock.ActionBar.TabView"> <item name="android:background">@drawable/tab_indicator</item> </style> <style name="Widget.Slider.ActionButton" parent="@style/Widget.Sherlock.ActionButton"> <item name="android:background">@drawable/selector_actionbar_button</item> <item name="background">@drawable/selector_actionbar_button</item> </style> <style name="TextAppearance.Slider.Widget.ActionBar.Titlen" parent="@style/TextAppearance.Sherlock.Widget.ActionBar.Title"> <item name="android:textColor">#eeeeee</item> <item name="android:textSize">20sp</item> </style>
大家把工程down下來仔細比對下就知道哪些欄位對應哪些UI效果
欲更多深入瞭解就多瞅瞅ActionBarSherklock工程裡的style檔案
自訂style繼承原生style再修改對應欄位就OK了
大家應該注意到style裡的欄位會有android首碼和無android首碼的欄位如
<style name="Widget.Slider.ActionBar" parent="@style/Widget.Sherlock.ActionBar"> <item name="android:background">@drawable/base_actionbar_bg</item> <item name="android:backgroundStacked">#ffeeeeee</item> <item name="android:titleTextStyle">@style/TextAppearance.Slider.Widget.ActionBar.Titlen</item> <item name="titleTextStyle">@style/TextAppearance.Slider.Widget.ActionBar.Titlen</item> <item name="background">@drawable/base_actionbar_bg</item> <item name="backgroundStacked">#ffeeeeee</item> </style>
這是因為2.x裡使用其sytle的時候用到的是無android首碼的欄位ITEM,3.0以上的則是用到原生的,所以二者都要設值,否則在不同的系統版本上就可能出不來效果
最後附上工程連結:
http://download.csdn.net/detail/geniuseoe2012/5535041
欲知後事如何,且聽下回分解
more brilliant,Please pay attention to my CSDN blog -->http://blog.csdn.net/geniuseoe2012