android - Actionbar 上的 MenuItem 的 使用細節和最佳化

來源:互聯網
上載者:User

標籤:

MenuItem是actionbar上的元素,ui設計的時候的很多用這個控制項。

使用:

1.編寫menu.xml資源檔

<span style="font-size:14px;"><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    tools:context=".MainActivity">    <item        android:id="@+id/action_inbox"        android:icon="@mipmap/ic_inbox_white"        android:title="Inbox"        app:showAsAction="always" /></menu></span>
可以設定icon檔案,但是顯示效果可能不是很好。

showAsAction代表是否在actinbar中出現,常用always,ifroom,never;


2.在action中載入:

<span style="font-size:14px;">    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_main, menu);        return true;    }</span>

注意,可以自訂按鈕的屬性,就像自訂控制項那樣,

首先編寫布局檔案:

<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?><ImageButton xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="?attr/actionBarSize"    android:layout_height="?attr/actionBarSize"    android:background="@drawable/btn_default_light"    android:src="@mipmap/ic_inbox_white" /></span>

然後只需要在action的oncreateMenu()方法中增加幾行代碼,找到這個actionitem,然後調用setActionView()方法。

<span style="font-size:14px;">    @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.menu_main, menu);        inBoxMenuItem = menu.findItem(R.id.action_inbox);        inBoxMenuItem.setActionView(R.layout.menu_item_view);        return true;    }</span>



android - Actionbar 上的 MenuItem 的 使用細節和最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.