用较早版本的APIs实现抽象类
原文鏈接:http://android.eoe.cn/topic/android_sdk
?
用較早版本的APIs實(shí)現(xiàn)抽象類
這節(jié)課程我們討論如何創(chuàng)建一個實(shí)現(xiàn)類,即能對應(yīng)新版本的API,又能夠保持對老版本API的支持。
尋找一個替代的解決方案
為了保持向后兼容,我們需要用老版本平臺的UI框架特性來實(shí)現(xiàn)新本版的UI框架特性,這是一件非常具有挑戰(zhàn)性的任務(wù)。在很多情況下,我們是完全可以做到這一點(diǎn)的,請看下邊的例子:
-
- Action bars能夠用一個 horizontalLinearLayout在你Activity Layout中實(shí)現(xiàn),這個LinearLayout可以添加一個自定義的標(biāo)題或者是Views,加上image buttons,執(zhí)行的動作可以在設(shè)備的Menu button來顯示。
-
- Action bar tabs可以用horizontal LinearLayout加上按鈕或者用TabWidget?UI來實(shí)現(xiàn)。
-
- NumberPicker和Switch部件可以用Spinner和ToggleButton部件分別實(shí)現(xiàn)。
-
- ListPopupWindow和PopupMenu部件可以用PopupWindow部件來實(shí)現(xiàn)。
一般情況下,我們無法找到一個完美的方案,可以把新UI組件的完全的移植到舊版本的設(shè)備上。在這個問題上我們應(yīng)該多考慮一下用戶體驗(yàn),使用老版本設(shè)備的用戶可能對新版本的設(shè)計(jì)模式并不熟悉。因此我們在實(shí)現(xiàn)的時(shí)候要考慮相同的功能實(shí)現(xiàn)盡量用用戶熟悉的方式來實(shí)現(xiàn)。很多情況下我們不必過于擔(dān)心這個問題,如果這個新的UI組件在應(yīng)用程序的環(huán)境中設(shè)計(jì)的比較優(yōu)秀(比如:'''Action Bar''')或者交互模式非常簡單和直觀的(比如:'''Swip Views'''應(yīng)用?ViewPager。
用較早版本API實(shí)現(xiàn)Tabs
我們可以用TabWidget和TabHost?我們也可以用horizontally laid-out?Button部件來實(shí)現(xiàn)'''ActionBar'''標(biāo)簽。因?yàn)槲覀兪褂昧薃ndroid 2.0 (Eclair)以下的APIs,所以我們的實(shí)現(xiàn)類名字叫做TabHelperEclair和CompatTabEclair:
圖1:Eclair實(shí)現(xiàn)tabs的類圖
實(shí)現(xiàn)CompatTabEclair時(shí)需要在變量中保存tab的屬性,比如:text、icon等。因?yàn)槲覀円呀?jīng)沒有現(xiàn)成的ActionBar.Tab可以幫助來處理這些屬性了。
| 123456789 10 11 12 13 14 15 16 | public class CompatTabEclair extends CompatTab {// Store these properties in the instance,// as there is no ActionBar.Tab object.private CharSequence mText;...public CompatTab setText(int resId) {// Our older implementation simply stores this// information in the object instance.mText = mActivity.getResources().getText(resId);return this;}...// Do the same for other properties (icon, callback, etc.) } |
實(shí)現(xiàn)TabHelperEclair時(shí)需要需要用到TabHost部件來創(chuàng)建TabHost.TabSpec對象和tab indicators:
| 123456789 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class TabHelperEclair extends TabHelper {private TabHost mTabHost;...protected void setUp() {if (mTabHost == null) {// Our activity layout for pre-Honeycomb devices// must contain a TabHost.mTabHost = (TabHost) mActivity.findViewById(android.R.id.tabhost);mTabHost.setup();}}public void addTab(CompatTab tab) {...TabSpec spec = mTabHost.newTabSpec(tag).setIndicator(tab.getText()); // And optional icon...mTabHost.addTab(spec);}// The other important method, newTab() is part of// the base implementation. } |
現(xiàn)在我們有CompatTab和TabHelper的兩個實(shí)現(xiàn)類了,一個用在Android 3.0或者更新的版本的APIs中,一個用在Android 2.0或更新版本的APIs中,下節(jié)課我們將討論如何在我們的應(yīng)用中使用這樣兩個實(shí)現(xiàn)類。
?
轉(zhuǎn)載于:https://www.cnblogs.com/vus520/p/3158862.html
總結(jié)
以上是生活随笔為你收集整理的用较早版本的APIs实现抽象类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 自我训练——时间控制能力(四)
- 下一篇: 取出重复记录的第一条