非常感谢您的建议!
附:我知道,我很跛脚:)(我为我的英文道歉)
启用下拉导航的基本步骤是:
1.创建一个SpinnerAdapter,它为列表中的每个项目提供下拉列表和可用项目列表.
2.实现Actionbar.OnNavigationListener来定义当用户从列表中选择一个项目时发生的行为.
3.在活动的onCreate()方法中,通过调用setNavigationMode(NAVIGATION_MODE_List)来启用 *** 作栏的下拉列表.
4.使用setListNavigationCallbacks()为下拉列表设置回调函数
http://developer.android.com/guide/topics/ui/actionbar.html
检查添加下拉导航的主题
例:
public class MainActivity extends Activity implements OnNavigationListener
然后
getActionbar().setdisplayShowTitleEnabled(false); getActionbar().setNavigationMode(Actionbar.NAVIGATION_MODE_List); SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromresource(this,R.array.planets_array,androID.R.layout.simple_spinner_dropdown_item); getActionbar().setListNavigationCallbacks(mSpinnerAdapter,this);
然后
@OverrIDepublic boolean onNavigationItemSelected(int arg0,long arg1) { Toast.makeText(getApplicationContext()," text",1000).show(); return true;}
快照
例:
public class MainActivity extends Activity implements Actionbar.OnNavigationListener { private DrawerLayout mDrawerLayout; private ListVIEw mDrawerList; public ActionbarDrawerToggle mDrawerToggle; private CharSequence mDrawerTitle; private CharSequence mTitle; private String[] mPlanetTitles; Actionbar actionbar; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); actionbar = getActionbar(); SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromresource(this,androID.R.layout.simple_spinner_dropdown_item); actionbar.setdisplayShowTitleEnabled(false); actionbar.setNavigationMode(Actionbar.NAVIGATION_MODE_List); getActionbar().setListNavigationCallbacks(mSpinnerAdapter,this); //mTitle = mDrawerTitle = getTitle(); mPlanetTitles = getResources().getStringArray(R.array.planets_array); mDrawerLayout = (DrawerLayout) findVIEwByID(R.ID.drawer_layout); mDrawerList = (ListVIEw) findVIEwByID(R.ID.left_drawer); // mDrawerList.setBackgroundcolor(color.WHITE); // 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 getActionbar().setdisplayHomeAsUpEnabled(true); getActionbar().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 */ ) { public voID onDrawerClosed(VIEw vIEw) { getActionbar().setTitle(mTitle); invalIDateOptionsMenu(); // creates call to onPrepareOptionsMenu() } public voID onDrawerOpened(VIEw drawerVIEw) { getActionbar().setTitle(mDrawerTitle); invalIDateOptionsMenu(); // creates call to onPrepareOptionsMenu() } }; mDrawerLayout.setDrawerListener(mDrawerToggle); if (savedInstanceState == null) { selectItem(0); } } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); 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 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,getActionbar().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); } } /* 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) { Actionbar actionbar =getActionbar(); actionbar.setdisplayHomeAsUpEnabled(true); Fragment fragment = new PlanetFragment(); Bundle args = new Bundle(); args.putInt(PlanetFragment.ARG_PLANET_NUMBER,position); fragment.setArguments(args); FragmentManager fragmentManager = getFragmentManager(); 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; //getActionbar().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"; private static WeakReference<MainActivity> mTarget; @OverrIDe public voID onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if(mTarget!=null) { MainActivity target = mTarget.get(); Actionbar actionbar = getActivity().getActionbar(); actionbar.setdisplayHomeAsUpEnabled(true); //actionbar.setTitle("Fragment"); //target.mDrawerToggle.setDrawerIndicatorEnabled(false); } else { Log.i("............","Null"); } } @OverrIDe public voID onAttach(Activity activity) { // Todo auto-generated method stub super.onAttach(activity); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { mTarget = new WeakReference<MainActivity>( (MainActivity) getActivity()); 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; } } @OverrIDe public boolean onNavigationItemSelected(int itemposition,long itemID) { // Todo auto-generated method stub Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_LONG).show(); return true; }}
activity_main.xml中
<androID.support.v4.Widget.DrawerLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/drawer_layout" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <!-- As the main content vIEw,the vIEw below consumes the entire space available using match_parent in both dimensions. --> <FrameLayout androID:ID="@+ID/content_frame" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /> <!-- androID:layout_gravity="start" tells DrawerLayout to treat this as a slIDing drawer on the left sIDe for left-to-right languages and on the right sIDe for right-to-left languages. The drawer is given a fixed wIDth in dp and extends the full height of the container. A solID background is used for contrast with the content vIEw. --> <ListVIEw androID:ID="@+ID/left_drawer" androID:layout_wIDth="240dp" androID:layout_height="match_parent" androID:layout_gravity="start" androID:choiceMode="singleChoice" androID:divIDer="@androID:color/transparent" androID:divIDerHeight="0dp" /></androID.support.v4.Widget.DrawerLayout>
drawe_List_item.xml
<TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@androID:ID/text1" androID:background="@drawable/selector" 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:minHeight="?androID:attr/ListPreferredItemHeightSmall"/>
fragment_planet.xml
<ImageVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/image" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#000000" androID:gravity="center" androID:padding="32dp" />总结
以上是内存溢出为你收集整理的android – 如何组合导航抽屉和微调[像Google App]全部内容,希望文章能够帮你解决android – 如何组合导航抽屉和微调[像Google App]所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)