Android選項(xiàng)菜單示例含代碼

    Android選項(xiàng)菜單示例

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

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

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

    Android選項(xiàng)菜單示例

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

    activity_main.xml

    在這個(gè)文件中只有一個(gè)文本視圖。愛(ài)掏網(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

    它包含如下所示的三個(gè)項(xiàng)目。愛(ài)掏網(wǎng) - it200.com它是在res/menu目錄內(nèi)自動(dòng)生成的。愛(à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)容并在點(diǎn)擊菜單項(xiàng)時(shí)執(zhí)行事件處理。愛(ài)掏網(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);
            }
        }
    }
    

    輸出:

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

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

    點(diǎn)擊第二個(gè)菜單項(xiàng)后的輸出。愛(ài)掏網(wǎng) - it200.com

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

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

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

    返回頂部

    主站蜘蛛池模板: 亚洲av乱码一区二区三区| 日韩免费一区二区三区| 日本免费一区二区三区| 无码国产精品一区二区免费模式| 久久人妻av一区二区软件| 国产成人无码一区二区三区在线| 久久久99精品一区二区| 国产综合无码一区二区辣椒| 无码少妇一区二区| 亚洲.国产.欧美一区二区三区| 国产精品亚洲专一区二区三区| 久久久国产一区二区三区| 在线观看亚洲一区二区| 日本精品视频一区二区| 红桃AV一区二区三区在线无码AV| 91一区二区视频| 国产伦理一区二区三区| 日本中文字幕一区二区有码在线| 亚洲一区二区三区高清不卡| 日韩视频在线一区| 中文字幕亚洲乱码熟女一区二区| 亚洲av无码一区二区三区观看| 国产成人精品一区二三区| 亚洲老妈激情一区二区三区| 亚洲熟女综合一区二区三区| 中文字幕不卡一区| 色婷婷av一区二区三区仙踪林| 精品一区二区三区在线观看| 国产一区二区成人| 国产午夜毛片一区二区三区| 无码一区18禁3D| 久久精品一区二区免费看| 午夜性色一区二区三区不卡视频 | 麻豆亚洲av熟女国产一区二| 波多野结衣精品一区二区三区| 久久久av波多野一区二区| 亚洲人成网站18禁止一区| 天堂va视频一区二区| 亚洲视频一区在线播放| 精品人妻一区二区三区毛片| 精品一区狼人国产在线|