Android學習:ActionBar活動條
阿新 • • 發佈:2018-03-17
roi oid idg info sel behavior prot short http
菜單定義:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:title="@string/font_size" android:showAsAction="always|withText" android:icon="@drawable/font"> <menu> <!-- 定義一組單選菜單項 --> <group android:checkableBehavior="single"> <!-- 定義多個菜單項 --> <item android:id="@+id/font_10" android:title="@string/font_10"/> <item android:id="@+id/font_12" android:title="@string/font_12"/> <item android:id="@+id/font_14" android:title="@string/font_14"/> <item android:id="@+id/font_16" android:title="@string/font_16"/> <item android:id="@+id/font_18" android:title="@string/font_18"/> </group> </menu> </item> <!-- 定義一個普通菜單項 --> <item android:id="@+id/plain_item" android:showAsAction="always|withText" android:title="@string/plain_item"> </item> <item android:title="@string/font_color" android:showAsAction="always" android:icon="@drawable/color"> <menu> <!-- 定義一組允許復選的菜單項 --> <group> <!-- 定義三個菜單項 --> <item android:id="@+id/red_font" android:title="@string/red_title"/> <item android:id="@+id/green_font" android:title="@string/green_title"/> <item android:id="@+id/blue_font" android:title="@string/blue_title"/> </group> </menu> </item> </menu>
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 定義一組單選菜單項目 --> <group android:checkableBehavior="single"> <!-- 定義三個菜單項 --> <item android:id="@+id/red" android:title="@string/red_title" android:alphabeticShortcut="r"/> <item android:id="@+id/green" android:title="@string/green_title" android:alphabeticShortcut="g"/> <item android:id="@+id/blue" android:title="@string/blue_title" android:alphabeticShortcut="b"/> </group> </menu>
界面定義:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="15pt" android:text="可通過上下文菜單修改背景色" /> </LinearLayout>
字符串資源:
<resources> <string name="app_name">ActionItemTest</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="red_title">紅色</string> <string name="blue_title">藍色</string> <string name="green_title">綠色</string> <string name="font_size">字體大小</string> <string name="font_color">字體顏色</string> <string name="plain_item">普通菜單項</string> <string name="font_10">10號字體</string> <string name="font_12">12號字體</string> <string name="font_14">14號字體</string> <string name="font_16">16號字體</string> <string name="font_18">18號字體</string> </resources>
代碼:
package allegro.actionbartes; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt = (TextView) findViewById(R.id.txt); // 為文本框註冊上下文菜單 registerForContextMenu(txt); } public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflator = new MenuInflater(this); //裝填R.menu.my_menu對應的菜單,並添加到menu中 inflator.inflate(R.menu.menu_main, menu); return super.onCreateOptionsMenu(menu); } // 創建上下文菜單時觸發該方法 @Override public void onCreateContextMenu(ContextMenu menu, View source, ContextMenu.ContextMenuInfo menuInfo) { MenuInflater inflator = new MenuInflater(this); //裝填R.menu.context對應的菜單,並添加到menu中 inflator.inflate(R.menu.context , menu); menu.setHeaderIcon(R.drawable.tools); menu.setHeaderTitle("請選擇背景色"); } // 上下文菜單中菜單項被單擊時觸發該方法 @Override public boolean onContextItemSelected(MenuItem mi) { // 勾選該菜單項 mi.setChecked(true); // ① switch (mi.getItemId()) { case R.id.red: mi.setChecked(true); txt.setBackgroundColor(Color.RED); break; case R.id.green: mi.setChecked(true); txt.setBackgroundColor(Color.GREEN); break; case R.id.blue: mi.setChecked(true); txt.setBackgroundColor(Color.BLUE); break; } return true; } @Override // 菜單項被單擊後的回調方法 public boolean onOptionsItemSelected(MenuItem mi) { if(mi.isCheckable()) { // 勾選該菜單項 mi.setChecked(true); // ② } //判斷單擊的是哪個菜單項,並有針對性地作出響應 switch (mi.getItemId()) { case R.id.font_10: txt.setTextSize(10 * 2); break; case R.id.font_12: txt.setTextSize(12 * 2); break; case R.id.font_14: txt.setTextSize(14 * 2); break; case R.id.font_16: txt.setTextSize(16 * 2); break; case R.id.font_18: txt.setTextSize(18 * 2); break; case R.id.red_font: txt.setTextColor(Color.RED); mi.setChecked(true); break; case R.id.green_font: txt.setTextColor(Color.GREEN); mi.setChecked(true); break; case R.id.blue_font: txt.setTextColor(Color.BLUE); mi.setChecked(true); break; case R.id.plain_item: Toast toast = Toast.makeText(MainActivity.this , "您單擊了普通菜單項" , Toast.LENGTH_SHORT); toast.show(); break; } return true; } }
Android學習:ActionBar活動條