一区二区日本_久久久久久久国产精品_无码国模国产在线观看_久久99深爱久久99精品_亚洲一区二区三区四区五区午夜_日本在线观看一区二区

Android選項菜單示例含代碼

Android選項菜單示例

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

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

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

Android選項菜單示例

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

activity_main.xml

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

它包含如下所示的三個項目。愛掏網 - it200.com它是在res/menu目錄內自動生成的。愛掏網 - 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文件的內容并在點擊菜單項時執行事件處理。愛掏網 - 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);
        }
    }
}

輸出:

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

點擊菜單按鈕后的輸出。愛掏網 - it200.com

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

菜單選項與圖標

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

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

返回頂部

主站蜘蛛池模板: 91精品国产91久久综合桃花 | 午夜精品久久久久久久 | 蜜桃精品在线 | 欧美a在线 | 亚洲a人| 久久一区二区视频 | 日韩精品在线看 | 视频三区| 精品视频一区二区三区在线观看 | 亚洲精品久久久蜜桃 | 五月香婷婷 | 2018天天干天天操 | 在线2区| 一区二区三区免费在线观看 | 久久久一区二区三区四区 | 欧美激情一区二区 | 欧美午夜精品久久久久免费视 | 黑人精品欧美一区二区蜜桃 | 一区二区三区四区在线 | 欧美精品一区二区三区四区 在线 | 一区二区三区欧美 | 日韩成人| 国内精品免费久久久久软件老师 | 久久久久国产精品 | 亚洲精品乱码 | 天天操天天操 | 欧美日韩综合一区 | 中文字幕在线视频精品 | 久久久不卡网国产精品一区 | 欧美美女爱爱 | 亚洲色图插插插 | 性色的免费视频 | 午夜视频在线免费观看 | 一区二区三区在线播放 | 色偷偷888欧美精品久久久 | 成人自拍av | 一区二区三区四区不卡视频 | 欧美一区二区三区在线看 | 草草精品 | 亚洲欧美一区二区三区视频 | 久久婷婷av|