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