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