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

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

在黃岡等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),網(wǎng)絡(luò)營(yíng)銷推廣,外貿(mào)網(wǎng)站制作,黃岡網(wǎng)站建設(shè)費(fèi)用合理。

使用 XML 文件定義菜單

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

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

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

具體實(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;
  }
}

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》

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

網(wǎng)頁(yè)名稱:Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
網(wǎng)站鏈接:http://muchs.cn/article36/gjgspg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、手機(jī)網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、定制開發(fā)、云服務(wù)器、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司