Android ActionBar Item用法 Android学习笔记之ActionBar Item用法分析
Flying_tao 人气:0想了解Android学习笔记之ActionBar Item用法分析的相关内容吗,Flying_tao在本文为您仔细讲解Android ActionBar Item用法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,ActionBar,Item,下面大家一起来学习吧。
本文实例讲述了Android ActionBar Item用法。分享给大家供大家参考,具体如下:
这里主要讲述ActionBar Item的使用方法。在手机上,按Menu键出现Menu菜单,但是在平板中可以把菜单放在ActionBar里面,放置的Item也比手机上多,下面详细介绍其用法:
1、在Androidmanifest.xml里面加上
<uses-sdk Android:minSdkVersion="11"/>
或者
<uses-sdk android:targetSdkVersion="11" />
要使版本号在3.0以上,这样系统就自动把Menu放在ActionBar上面。
2、准备一个menu.xml,以便加载。其实这就是一个传统的menu布局,只是多了android:showAsAction="ifRoom|withText"
这个属性,ifRoom的意思就是说只要ActionBar上有空间,就把该Item显示出来,否则就坠在后面。
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_search" android:title="action_bar_search" android:showAsAction="ifRoom" android:actionViewClass="android.widget.SearchView" /> <item android:id="@+id/action_add" android:icon="@android:drawable/ic_menu_add" android:title="action_bar_add" /> <item android:id="@+id/action_edit" android:icon="@android:drawable/ic_menu_edit" android:showAsAction="always" android:title="action_bar_edit" /> <item android:id="@+id/action_share" android:icon="@android:drawable/ic_menu_share" android:title="action_bar_share" android:showAsAction="ifRoom" /> <item android:id="@+id/action_sort" android:icon="@android:drawable/ic_menu_sort_by_size" android:title="action_bar_sort" android:showAsAction="ifRoom"> <menu> <item android:id="@+id/action_sort_size" android:icon="@android:drawable/ic_menu_sort_by_size" android:title="action_bar_sort_size" android:onClick="onSort" /> <item android:id="@+id/action_sort_alpha" android:icon="@android:drawable/ic_menu_sort_alphabetically" android:title="action_bar_sort_alpha" android:onClick="onSort" /> </menu> </item> </menu>
3、Activity中的代码:
import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.widget.SearchView; import android.widget.SearchView.OnQueryTextListener; import android.widget.TextView; import android.widget.Toast; /** * This demonstrates idiomatic usage of the Action Bar. The default Honeycomb theme * includes the action bar by default and a menu resource is used to populate the * menu data itself. If you'd like to see how these things work under the hood, see * ActionBarMechanics. */ public class ActionBarUsageActivity extends Activity implements OnQueryTextListener { TextView mSearchText; int mSortMode = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSearchText = new TextView(this); setContentView(mSearchText); } //和加载传统的menu一样,重写onCreateOptionsMenu方法 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actions, menu); SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); searchView.setOnQueryTextListener(this); return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { if (mSortMode != -1) { Drawable icon = menu.findItem(mSortMode).getIcon(); menu.findItem(R.id.action_sort).setIcon(icon); } return super.onPrepareOptionsMenu(menu); } //和相应传统的menu一样,重写onOptionsItemSelected方法 @Override public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show(); return true; } // This method is specified as an onClick handler in the menu xml and will // take precedence over the Activity's onOptionsItemSelected method. // See res/menu/actions.xml for more info. public void onSort(MenuItem item) { mSortMode = item.getItemId(); // Request a call to onPrepareOptionsMenu so we can change the sort icon invalidateOptionsMenu(); } // The following callbacks are called for the SearchView.OnQueryChangeListener // For more about using SearchView, see src/.../view/SearchView1.java and SearchView2.java public boolean onQueryTextChange(String newText) { newText = newText.isEmpty() ? "" : "Query so far: " + newText; mSearchText.setText(newText); return true; } public boolean onQueryTextSubmit(String query) { Toast.makeText(this, "Searching for: " + query + "...", Toast.LENGTH_SHORT).show(); return true; } }
这样,一个ActionBar Item的例子就顺利的完成了。
希望本文所述对大家Android程序设计有所帮助。
加载全部内容