麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 系統(tǒng) > Android > 正文

Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例

2019-10-21 21:19:21
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了Android使用xml文件資源定義菜單實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

使用 XML 文件定義菜單

Android 提供了創(chuàng)建菜單的方式,一種是在 Java 代碼中創(chuàng)建,一種是使用XML 文件定義。上面的實(shí)例都是 Java 創(chuàng)建菜單,在 Java 存在如下大學(xué)。

實(shí)現(xiàn)效果如下:

Android,xml,文件資源,定義菜單

具體實(shí)現(xiàn):

一、在 /res 下建立 /menu文件夾

二、在menu文件夾下建立:menu_main.xml:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">  <item android:title="@string/app_name"    android:icon="@drawable/seek02">    <menu>      <!--定義一組選項(xiàng)菜單-->      <group android:checkableBehavior="single">        <!--定義多個(gè)菜單項(xiàng)-->        <item          android:id="@+id/font_10"          android:title="font_10"/>        <item          android:id="@+id/font_12"          android:title="font_12"/>        <item          android:id="@+id/font_14"          android:title="font_14"/>        <item          android:id="@+id/font_16"          android:title="font_16"/>        <item          android:id="@+id/font_18"          android:title="font_18"/>      </group>    </menu>  </item>  <!--定義一個(gè)普通菜單項(xiàng)-->  <item android:id="@+id/plain_item"    android:title="plain_item"/>  <item android:title="font_color"    android:icon="@drawable/seek03">    <menu>      <!--定義一個(gè)普通選項(xiàng)菜單-->      <group>        <!--定義三個(gè)菜單項(xiàng)-->        <item          android:id="@+id/red_font"          android:title="red_title"/>        <item          android:id="@+id/green_font"          android:title="red_title"/>        <item          android:id="@+id/blue_font"          android:title="red_title"/>      </group>    </menu>  </item></menu>

三、在menu文件夾下建立: context.xml:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">  <!--定義一組單選菜單項(xiàng)-->  <group android:checkableBehavior="single">    <!--定義三個(gè)菜單項(xiàng)-->    <item      android:id="@+id/red"      android:title="red_title"      android:alphabeticShortcut="r"/>    <item      android:id="@+id/green"      android:title="red_title"      android:alphabeticShortcut="g"/>    <item      android:id="@+id/blue"      android:title="red_title"      android:alphabeticShortcut="b"/>  </group></menu>

四、主活動(dòng)里的實(shí)現(xiàn):

public class MainActivity extends AppCompatActivity {  private TextView textView;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    textView = (TextView) findViewById(R.id.txt);    // 為文本框注冊(cè)上下文菜單    registerForContextMenu(textView);  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    MenuInflater inflater = new MenuInflater(this);    //裝填R.Menu.my_menu菜單,并添加到menu中    inflater.inflate(R.menu.menu_main,menu);    return super.onCreateOptionsMenu(menu);  }  //創(chuàng)建上下文菜單時(shí)觸發(fā)該方法  @Override  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {    MenuInflater inflater = new MenuInflater(this);    //裝填R.Menu.menu菜單,并添加到menu中    inflater.inflate(R.menu.context,menu);    menu.setHeaderIcon(R.drawable.seek02);    menu.setHeaderTitle("請(qǐng)選擇背景色");  }  //上下文菜單中菜單項(xiàng)被單擊時(shí),觸發(fā)該方法  @Override  public boolean onContextItemSelected(MenuItem item) {    //勾選菜單項(xiàng)    item.setChecked(true);    switch (item.getItemId()){      case R.id.red:        item.setChecked(true);        textView.setBackgroundColor(Color.RED);        break;      case R.id.green:        item.setChecked(true);        textView.setBackgroundColor(Color.GREEN);        break;      case R.id.blue:        item.setChecked(true);        textView.setBackgroundColor(Color.BLUE);        break;    }    return true;  }  //菜單項(xiàng)被單擊后的回調(diào)方法  @Override  public boolean onOptionsItemSelected(MenuItem item) {    if (item.isCheckable()){      //勾選菜單項(xiàng)      item.setCheckable(true);    }    //switch 判斷單擊哪個(gè)菜單項(xiàng),并有針對(duì)性的做出響應(yīng)    switch (item.getItemId()){      case R.id.font_10:        textView.setTextSize(10 * 2);        break;      case R.id.font_12:        textView.setTextSize(12 * 2);        break;      case R.id.font_14:        textView.setTextSize(14 * 2);        break;      case R.id.font_16:        textView.setTextSize(16 * 2);        break;      case R.id.font_18:        textView.setTextSize(18 * 2);        break;      case R.id.red_font:        textView.setTextColor(Color.RED);        break;      case R.id.green_font:        textView.setTextColor(Color.GREEN);        break;      case R.id.blue_font:        textView.setTextColor(Color.BLUE);        break;    }    return true;  }}

 

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产激情网 | 久久久精品视频免费 | 欧美一级色片 | 国产影视| 国产亚洲精品久久久久5区 男人天堂免费 | 懂色粉嫩av久婷啪 | 久久久免费观看完整版 | 中文字幕一区久久 | 久久精精 | 久久久综合 | 欧美成网站 | 蜜桃视频观看麻豆 | 久色成人 | 亚洲欧美在线视频免费 | 久色免费 | 未成年人在线观看 | 操碰97 | 久久国产免费 | 国产色视频免费 | 免费观看黄色一级视频 | 国产精品午夜未成人免费观看 | 蜜桃传媒视频麻豆第一区免费观看 | 国产成人高潮免费观看精品 | 欧美日韩视频第一页 | 久久久久久久久久一本门道91 | 国产一区二区三区网站 | 久久久国产电影 | h视频免费看 | 国产精品久久久久久久不卡 | 黄片毛片一级 | 91成人午夜性a一级毛片 | 久久国产精品电影 | 欧美黑人伦理 | 在线观看国产免费视频 | 免费毛片免费看 | 在线天堂中文字幕 | 亚洲爱爱图 | 成人福利视频在线观看 | 国产精品一区二区手机在线观看 | 粉嫩粉嫩一区二区三区在线播放 | xxxx69hd一hd |