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; @Override
protected 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 opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.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 drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true); // ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = 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);
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
} /* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
} @Override
public 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 buttons
switch(item.getItemId()) {
case android.R.id.home:
handleNavigationDrawerToggle();
return true;
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getSupportActionBar().getTitle());
// catch event that there's no activity to handle intent
if (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 {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
} private void selectItem(int position) {
// update the main content by replacing fragments
Fragment 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 drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} @Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
} /**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/ @Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
} @Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.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
} @Override
public 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 replaced
by 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 in
res/values-vXX/styles.xml, while customizations related to
backward-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 replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/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了。
代码下载:Demo
Android UI开发第三十三篇——Navigation Drawer For Android API 7的更多相关文章
- Android UI开发第三十篇——使用Fragment构建灵活的桌面
http://www.lupaworld.com/article-222973-1.html 当我们设计应用程序时,希望能够尽最大限度的适配各种设备,包括4寸屏.7寸屏. 10寸屏等等,Android ...
- Android UI开发第三十一篇——Android的Holo Theme
好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...
- 【转】Android UI开发第三十一篇——Android的Holo Theme
好长时间没写Android UI方面的文章了,今天就闲扯一下Android的Holo主题.一直做android开发的可能都知道,Android 系统的UI有过两次大的变化,一次是android 3.0 ...
- Android UI开发第四十三篇——使用Property Animation实现墨迹天气3.0引导界面及动画实现
前面写过<墨迹天气3.0引导界面及动画实现>,里面完美实现了动画效果,那一篇文章使用的View Animation,这一篇文章使用的Property Animation实现.Propert ...
- Android UI开发第三十九篇——Tab界面实现汇总及比较
Tab布局是iOS的经典布局,Android应用中也有大量应用,前面也写过Android中TAb的实现,<Android UI开发第十八篇——ActivityGroup实现tab功能>.这 ...
- Android UI开发第三十五篇——AppCompat实现Action Bar
每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...
- Android UI开发第二十八篇——Fragment中使用左右滑动菜单
Fragment实现了Android UI的分片管理,尤其在平板开发中,好处多多.这一篇将借助Android UI开发第二十六篇——Fragment间的通信. Android UI开发第二十七篇——实 ...
- Android UI开发第三十二篇——Creating a Navigation Drawer
Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...
- Android UI开发第三十四篇——SlidingPaneLayout
SlidingPaneLayout也是系统支持的高级控件,是Android团对在2013 google IO大会期间更新的Support库(Version 13)中新加入的重要的功能.它支持左右滑动菜 ...
随机推荐
- Unity C#写的A*寻路
原地址:http://www.unity蛮牛.com/blog-13769-1078.html 首先看了这篇翻译外国人的文章http://www.raywenderlich.com/zh-hans/2 ...
- POJ 3308 Paratroopers(最小割EK)
题目链接 题意 : 有一个n*m的矩阵,L个伞兵可能落在某些点上,这些点的坐标已知,需要在某些位置安上一些枪,然后每个枪可以将一行或者一列的伞兵击毙.把这种枪安装到不同行的行首.或者不同列的列首,费用 ...
- linux zip 命令详解
功能说明:压缩文件. 语 法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时 ...
- 在8086中,[ idata],[bx]表示内存单元时。可能是一个字节,也可能是一个字。
可能表示一个字节,也可能表示一个字.主要由指令中另一个计算对象决定.如al表示一个字节.ax就表示一个字. 这个区别主要体现在循环中,偏移地址的循环变量是加1还是加2,al是偏移地址加1,ax是偏移地 ...
- 【HDOJ】1262 寻找素数对
典型的二分决策树.而且本身两数和是偶数. #include <stdio.h> #include <string.h> #define MAXNUM 10001 int isP ...
- c#继承中的函数调用
首先看下面的代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 using System; namespace Test { public cl ...
- [转] Manacher算法详解
转载自: http://blog.csdn.net/dyx404514/article/details/42061017 Manacher算法 算法总结第三弹 manacher算法,前面讲了两个字符串 ...
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...
- 基于DDD的现代ASP.NET开发框架--ABP系列之3、ABP分层架构
基于DDD的现代ASP.NET开发框架--ABP系列之3.ABP分层架构 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ABP的官方网站:ht ...
- 分布式缓存系统Memcached简介与实践(.NET memcached client library)
缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵 ...