您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關(guān)ActionBar Item如何在Android應(yīng)用中使用,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
1、在Androidmanifest.xml里面加上
<uses-sdk Android:minSdkVersion="11"/>
或者
<uses-sdk android:targetSdkVersion="11" />
要使版本號在3.0以上,這樣系統(tǒng)就自動把Menu放在ActionBar上面。
2、準(zhǔn)備一個menu.xml,以便加載。其實這就是一個傳統(tǒng)的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); } //和加載傳統(tǒng)的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); } //和相應(yīng)傳統(tǒng)的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; } }
關(guān)于ActionBar Item如何在Android應(yīng)用中使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。