Android UI开发第三十三篇——Navigation Drawer For Android API 7
? ??Creating a Navigation Drawer中使用的Navigation Drawer的android:minSdkVersion="14",現在Android API Version 小于14的還有30%左右呢。Android個版本的占有量可參考 Platform Version。
Android Platform Version
?
? ? 我們的應用為了適配不同的版本,需要降低minSdkVersion,作者一般適配SDK到API 7。這一篇博文也是把Creating a Navigation Drawer的例子適配到API 7 。例子中降低API最主要的修改Action Bar,Action Bar是API 11才出現的,適配到API 7,我們使用了actionbarsherlock.
?
修改后的代碼:
?
public class MainActivity extends SherlockFragmentActivity {private DrawerLayout mDrawerLayout;private ListView mDrawerList;private ActionBarDrawerToggle mDrawerToggle;private CharSequence mDrawerTitle;private CharSequence mTitle;private String[] mPlanetTitles;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTitle = mDrawerTitle = getTitle();mPlanetTitles = getResources().getStringArray(R.array.planets_array);mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);mDrawerList = (ListView) findViewById(R.id.left_drawer);// set a custom shadow that overlays the main content when the drawer opensmDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);// set up the drawer's list view with items and click listenermDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item, mPlanetTitles));mDrawerList.setOnItemClickListener(new DrawerItemClickListener());// enable ActionBar app icon to behave as action to toggle nav drawergetSupportActionBar().setDisplayHomeAsUpEnabled(true);getSupportActionBar().setHomeButtonEnabled(true);// ActionBarDrawerToggle ties together the the proper interactions// between the sliding drawer and the action bar app iconmDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */mDrawerLayout, /* DrawerLayout object */R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */R.string.drawer_open, /* "open drawer" description for accessibility */R.string.drawer_close /* "close drawer" description for accessibility */) {/** Called when a drawer has settled in a completely closed state. */ public void onDrawerClosed(View view) { getSupportActionBar().setTitle(mTitle); } /** Called when a drawer has settled in a completely open state. */ public void onDrawerOpened(View drawerView) { getSupportActionBar().setTitle(mDrawerTitle); } };mDrawerLayout.setDrawerListener(mDrawerToggle);if (savedInstanceState == null) {selectItem(0);}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getSupportMenuInflater();inflater.inflate(R.menu.main, menu);return super.onCreateOptionsMenu(menu);}/* Called whenever we call invalidateOptionsMenu() */@Overridepublic boolean onPrepareOptionsMenu(Menu menu) {// If the nav drawer is open, hide action items related to the content viewboolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);return super.onPrepareOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// The action bar home/up action should open or close the drawer.// ActionBarDrawerToggle will take care of this. // if (mDrawerToggle.onOptionsItemSelected(item)) { // return true; // }// Handle action buttonsswitch(item.getItemId()) {case android.R.id.home: handleNavigationDrawerToggle();return true;case R.id.action_websearch:// create intent to perform web search for this planetIntent intent = new Intent(Intent.ACTION_WEB_SEARCH);intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());// catch event that there's no activity to handle intentif (intent.resolveActivity(getPackageManager()) != null) {startActivity(intent);} else {Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();}return true;default:return super.onOptionsItemSelected(item);}}private void handleNavigationDrawerToggle() {if (mDrawerLayout.isDrawerOpen(mDrawerList)) {mDrawerLayout.closeDrawer(mDrawerList);} else {mDrawerLayout.openDrawer(mDrawerList);}}/* The click listner for ListView in the navigation drawer */private class DrawerItemClickListener implements ListView.OnItemClickListener {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {selectItem(position);}}private void selectItem(int position) {// update the main content by replacing fragmentsFragment fragment = new PlanetFragment();Bundle args = new Bundle();args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);fragment.setArguments(args);FragmentManager fragmentManager = getSupportFragmentManager();fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();// update selected item and title, then close the drawermDrawerList.setItemChecked(position, true);setTitle(mPlanetTitles[position]);mDrawerLayout.closeDrawer(mDrawerList);}@Overridepublic void setTitle(CharSequence title) {mTitle = title;getSupportActionBar().setTitle(mTitle);}/*** When using the ActionBarDrawerToggle, you must call it during* onPostCreate() and onConfigurationChanged()...*/@Overrideprotected void onPostCreate(Bundle savedInstanceState) {super.onPostCreate(savedInstanceState);// Sync the toggle state after onRestoreInstanceState has occurred.mDrawerToggle.syncState();}@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);// Pass any configuration change to the drawer togglsmDrawerToggle.onConfigurationChanged(newConfig);}/*** Fragment that appears in the "content_frame", shows a planet*/public static class PlanetFragment extends Fragment {public static final String ARG_PLANET_NUMBER = "planet_number";public PlanetFragment() {// Empty constructor required for fragment subclasses}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.fragment_planet, container, false);int i = getArguments().getInt(ARG_PLANET_NUMBER);String planet = getResources().getStringArray(R.array.planets_array)[i];int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),"drawable", getActivity().getPackageName());((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);getActivity().setTitle(planet);return rootView;}} }
? ? ?如果只修改JAVA代碼,運行應用還是會出現異常:
?
?
08-13 13:26:15.909: E/AndroidRuntime(6832): FATAL EXCEPTION: main 08-13 13:26:15.909: E/AndroidRuntime(6832): android.view.InflateException: Binary XML file line #2: Error inflating class <unknown> 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.LayoutInflater.createView(LayoutInflater.java:518) 08-13 13:26:15.909: E/AndroidRuntime(6832): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.LayoutInflater.inflate(LayoutInflater.java:386) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.AbsListView.obtainView(AbsListView.java:1495) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.ListView.onMeasure(ListView.java:1127) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.View.measure(View.java:8335) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.View.measure(View.java:8335) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.View.measure(View.java:8335) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.View.measure(View.java:8335) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.View.measure(View.java:8335) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.ViewRoot.performTraversals(ViewRoot.java:843) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.ViewRoot.handleMessage(ViewRoot.java:1892) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.os.Handler.dispatchMessage(Handler.java:99) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.os.Looper.loop(Looper.java:130) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.app.ActivityThread.main(ActivityThread.java:3835) 08-13 13:26:15.909: E/AndroidRuntime(6832): at java.lang.reflect.Method.invokeNative(Native Method) 08-13 13:26:15.909: E/AndroidRuntime(6832): at java.lang.reflect.Method.invoke(Method.java:507) 08-13 13:26:15.909: E/AndroidRuntime(6832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864) 08-13 13:26:15.909: E/AndroidRuntime(6832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 08-13 13:26:15.909: E/AndroidRuntime(6832): at dalvik.system.NativeStart.main(Native Method) 08-13 13:26:15.909: E/AndroidRuntime(6832): Caused by: java.lang.reflect.InvocationTargetException 08-13 13:26:15.909: E/AndroidRuntime(6832): at java.lang.reflect.Constructor.constructNative(Native Method) 08-13 13:26:15.909: E/AndroidRuntime(6832): at java.lang.reflect.Constructor.newInstance(Constructor.java:415) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.LayoutInflater.createView(LayoutInflater.java:505) 08-13 13:26:15.909: E/AndroidRuntime(6832): ... 32 more 08-13 13:26:15.909: E/AndroidRuntime(6832): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.view.View.<init>(View.java:1978) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.TextView.<init>(TextView.java:350) 08-13 13:26:15.909: E/AndroidRuntime(6832): at android.widget.TextView.<init>(TextView.java:343) 08-13 13:26:15.909: E/AndroidRuntime(6832): ... 35 more
這個異常是 drawer_list_item.xml文件的內容產生的,drawer_list_item.xml內容:
?
?
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/text1"android:layout_width="match_parent"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceListItemSmall"android:gravity="center_vertical"android:paddingLeft="16dp"android:paddingRight="16dp"android:textColor="#fff"android:background="?android:attr/activatedBackgroundIndicator"android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
而在API 14之前的版本中,下面的三個屬性會報異常:
?
?
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:textAppearance="?android:attr/textAppearanceListItemSmall"android:background="?android:attr/activatedBackgroundIndicator"android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
如果想適配更低的版本需要做如下的修改:
?
1)修改drawer_list_item.xml文件,使用自定義的屬性
?
<?xml version="1.0" encoding="UTF-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@android:id/text1"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="?attr/myapp_activatedBackgroundIndicator"android:gravity="center_vertical"android:minHeight="?attr/myapp_listPreferredItemHeightSmall"android:paddingLeft="16dp"android:paddingRight="16dp"android:textAppearance="?attr/myapp_textAppearanceListItemSmall" />
2)在 values/attrs.xml文件中聲明自定義屬性
?
?
?
<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="AppTheme"><!-- Attributes below are needed to support the navigation drawer on Android 3.x. --><!-- A smaller, sleeker list item height. --><attr name="myapp_listPreferredItemHeightSmall" format="dimension" /><!-- Drawable used as a background for activated items. --><attr name="myapp_activatedBackgroundIndicator" format="reference" /><!-- The preferred TextAppearance for the primary text of small list items. --><attr name="myapp_textAppearanceListItemSmall" format="reference" /> </declare-styleable> </resources>
3) 在values-11/styles.xml文件中添加
?
?
<!--Base application theme, dependent on API level. This theme is replacedby AppBaseTheme from res/values-vXX/styles.xml on newer devices.--><style name="AppBaseTheme" parent="Theme.Sherlock.Light.DarkActionBar"><!--Theme customizations available in newer API levels can go inres/values-vXX/styles.xml, while customizations related tobackward-compatibility can go here.--><!-- Implementation of attributes needed for the navigation drawer as the default implementation is based on API-14. --><item name="myapp_listPreferredItemHeightSmall">48dip</item><item name="myapp_textAppearanceListItemSmall">@style/MyappDrawerMenu</item><item name="myapp_activatedBackgroundIndicator">@android:color/transparent</item></style><style name="MyappDrawerMenu"><item name="android:textSize">16sp</item><item name="android:textStyle">bold</item><item name="android:textColor">@android:color/black</item></style>
4) 在values-v14/styles.xml文件中添加
?
?
<!--Base application theme for API 14+. This theme completely replacesAppBaseTheme from BOTH res/values/styles.xml andres/values-v11/styles.xml on API 14+ devices.--><style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"><!-- For API-14 and above the default implementation of the navigation drawer menu is used. Below APU-14 a custom implementation is used. --><item name="myapp_listPreferredItemHeightSmall">?android:attr/listPreferredItemHeightSmall</item><item name="myapp_textAppearanceListItemSmall">?android:attr/textAppearanceListItemSmall</item><item name="myapp_activatedBackgroundIndicator">?android:attr/activatedBackgroundIndicator</item></style>?
OK,經過上面步驟,運行成功,這樣上一篇的例子就能很好的適配到API 7了。
? ? ? ? ?
?
?
/** * @author 張興業 * ?http://blog.csdn.net/xyz_lmn * ?iOS入門群:83702688* ?android開發進階群:241395671 * ?我的新浪微博:@張興業TBOW */
?
?
代碼下載:Demo
?
?
轉載于:https://www.cnblogs.com/pangblog/p/3364695.html
總結
以上是生活随笔為你收集整理的Android UI开发第三十三篇——Navigation Drawer For Android API 7的全部內容,希望文章能夠幫你解決所遇到的問題。

- 上一篇: 如何防止家中电器电击引发火灾?
- 下一篇: TQ210裸机编程(2)——LED流水灯