SubMenu(子選單),android 中點擊子功能表將彈出一個顯示子功能表項的懸浮框, 子功能表不支持嵌套,即不能包括其他子功能表。
Android Studio Menu 選單內增加 SubMenu 子選單 |
- 操作系統:Windows 7 64-bit 版本
- 開發環境:Android Studio 4.0.1 版本
- Gradle 版本:6.1.1
- 手機測試版本:API 19
- 原程式:C:\DIY-Android-181-13 OptionsMenuSubMenu bugworkshop
- 程式:C:\DIY-Android-181-13 OptionsMenuSubMenu bugworkshop
options_menu.xml:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search" android:orderInCategory="100" android:title="Search" android:icon="@drawable/ic_1" app:showAsAction="always|withText" />
<item android:id="@+id/action_settings" android:orderInCategory="100" android:title="Setting" android:icon="@android:drawable/ic_menu_set_as" app:showAsAction="never" >
<menu> <item android:id="@+id/action_setting1" android:orderInCategory="100" android:title="Setting1" android:icon="@android:drawable/ic_menu_set_as" app:showAsAction="never" />
<item android:id="@+id/action_setting2" android:orderInCategory="100" android:title="Setting2" android:icon="@android:drawable/ic_menu_set_as" app:showAsAction="never" /> </menu>
</item>
<item android:id="@+id/action_about" android:orderInCategory="100" app:showAsAction="never" android:title="About"/>
</menu> |
MainActivity.java:
package bugworkshop.optionsmenu;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.view.menu.MenuBuilder; import android.annotation.SuppressLint; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; public class MainActivity extends AppCompatActivity { final private int Help= 110; final private int Version= 111; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @SuppressLint("RestrictedApi") @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.options_menu, menu); menu.add(1,Help,4,"Help").setIcon(android.R.drawable.ic_menu_help); menu.add(1,Version,2,"Version").setIcon(android.R.drawable.ic_menu_info_details);; if (menu instanceof MenuBuilder) ((MenuBuilder) menu).setOptionalIconsVisible(true); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { Toast.makeText(this, "Selected Item: " + item.getTitle() + " Id=" + item.getGroupId() +","+item.getItemId(), Toast.LENGTH_SHORT).show(); switch (item.getItemId()) { case R.id.action_settings: // do your code return true; case R.id.action_about: // do your code return true; default: return super.onOptionsItemSelected(item); } } } |
Android Studio Menu 選單內增加 SubMenu 子選單 |
沒有留言:
張貼留言