Android選項菜單示例含代碼

Android選項菜單示例

Android選項菜單 是Android的主要菜單。愛掏網(wǎng) - it200.com它們可以用于設(shè)置、搜索、刪除項目等功能。愛掏網(wǎng) - it200.com

在這里,我們將看到兩個選項菜單的例子。愛掏網(wǎng) - it200.com首先是簡單的選項菜單,其次是帶有圖像的選項菜單。愛掏網(wǎng) - it200.com

在這里,我們通過調(diào)用 MenuInflater 類的 inflate() 方法來填充菜單。愛掏網(wǎng) - it200.com要對菜單項進行事件處理,需要覆蓋Activity類的 onOptionsItemSelected() 方法。愛掏網(wǎng) - it200.com

Android選項菜單示例

讓我們看看如何在Android中創(chuàng)建菜單。愛掏網(wǎng) - it200.com讓我們看一個包含三個菜單項的簡單選項菜單示例。愛掏網(wǎng) - it200.com

activity_main.xml

在這個文件中只有一個文本視圖。愛掏網(wǎng) - it200.com

<?xml version="1.0" encoding="utf-8"?>  
<android.support.design.widget.CoordinatorLayout 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"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="example.javatpoint.com.optionmenu.MainActivity">  

    <android.support.design.widget.AppBarLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:theme="@style/AppTheme.AppBarOverlay">  

        <android.support.v7.widget.Toolbar  
            android:id="@+id/toolbar"  
            android:layout_width="match_parent"  
            android:layout_height="?attr/actionBarSize"  
            android:background="?attr/colorPrimary"  
            app:popupTheme="@style/AppTheme.PopupOverlay" />  

    </android.support.design.widget.AppBarLayout>  

    <include layout="@layout/content_main" />  

</android.support.design.widget.CoordinatorLayout>  

context_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout 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"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  
    tools:context="example.javatpoint.com.optionmenu.MainActivity"  
    tools:showIn="@layout/activity_main">  

    <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Hello World!"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />  

</android.support.constraint.ConstraintLayout>  

menu_main.xml

它包含如下所示的三個項目。愛掏網(wǎng) - it200.com它是在res/menu目錄內(nèi)自動生成的。愛掏網(wǎng) - it200.com

menu_main.xml

<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="example.javatpoint.com.optionmenu.MainActivity">  

    <item  android:id="@+id/item1"  
        android:title="Item 1"/>  
    <item  android:id="@+id/item2"  
        android:title="Item 2"/>  
    <item  android:id="@+id/item3"  
        android:title="Item 3"  
        app:showAsAction="withText"/>  
</menu>  

Activity類

此類顯示menu.xml文件的內(nèi)容并在點擊菜單項時執(zhí)行事件處理。愛掏網(wǎng) - it200.com

package example.javatpoint.com.optionmenu;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
       int id = item.getItemId();
        switch (id){
            case R.id.item1:
                Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
                return true;
            case R.id.item2:
                Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();
                return true;
            case R.id.item3:
                Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

輸出:

在不點擊菜單按鈕的情況下輸出。愛掏網(wǎng) - it200.com

點擊菜單按鈕后的輸出。愛掏網(wǎng) - it200.com

點擊第二個菜單項后的輸出。愛掏網(wǎng) - it200.com

菜單選項與圖標(biāo)

您需要在res/drawable目錄下放置圖標(biāo)圖像。愛掏網(wǎng) - it200.comandroid:icon元素用于在選項菜單中顯示圖標(biāo)。愛掏網(wǎng) - it200.com您可以將字符串信息寫在strings.xml文件中。愛掏網(wǎng) - it200.com但我們將其寫在menu_main.xml文件中。愛掏網(wǎng) - it200.com

聲明:所有內(nèi)容來自互聯(lián)網(wǎng)搜索結(jié)果,不保證100%準(zhǔn)確性,僅供參考。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。
發(fā)表評論
更多 網(wǎng)友評論0 條評論)
暫無評論

返回頂部

主站蜘蛛池模板: freehd麻豆| 久久精品视频热| 久久五月天综合网| 日韩伦理电影在线免费观看| 国产在线不卡视频| 中文字幕高清在线| 第一福利社区导航| 日韩视频在线观看| 国产一级片免费看| 久久精品日日躁精品| 老司机亚洲精品| 大桥未久全63部作品番号| 亚洲欧洲日产国码AV系列天堂| tstye.cn| 欧美大片va欧美在线播放| 国产强被迫伦姧在线观看无码| 中文字幕不卡在线高清| 狠狠躁夜夜躁人人爽天天不卡软件| 国产精品永久久久久久久久久| 久久天天躁狠狠躁夜夜躁综合| 精品国产一区二区三区香蕉| 国产肥老上视频| 久久夜色精品国产网站| 粉嫩极品国产在线观看| 国产精品午夜高清在线观看 | 久久久久久久综合狠狠综合| 精品乱码一区二区三区四区| 国产综合成人久久大片91| 久久国产精品一国产精品| 精品91一区二区三区| 国产精品国产三级国产AV主播| 久久久久亚洲Av片无码下载蜜桃| 理论片在线观看免费| 国产成人精品无缓存在线播放| 一级毛片大全免费播放下载| 欧美日韩一区二区综合在线视频| 国产亚洲精品自在久久| 99在线热视频只有精品免费| 日韩伦理片电影在线免费观看| 免费大片黄手机在线观看| 欧美人与物另类|