2014-05-13 82 views
1

RES /菜單/ mainmenu.xml爲什麼我ActionButton將不會出現在我的動作條

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

<item 
    android:id="@+id/action_settings" 
    android:logo="@drawable/ic_action_settings" 
    android:showAsAction="ifRoom|withText" 
    android:title="@string/action_settings"/> 

<item 
    android:id="@+id/action_logout" 
    android:logo="@drawable/logout" 
    android:showAsAction="ifRoom|withText" 
    android:title="@string/action_logout"/> 


</menu> 

MainMenu.java 公共類MainMenu的擴展ActionBarActivity {

Button userinfo, requestservice, makepayment, trackparcel, checkcard; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_menu); 
     // Show the Up button in the action bar. 
     // setupActionBar(); 
     ActionBar actionBar = getSupportActionBar(); 
     actionBar.setDisplayHomeAsUpEnabled(false); 

     userinfo = (Button) findViewById(R.id.userinfo); 
     requestservice = (Button) findViewById(R.id.requestservice); 
     makepayment = (Button) findViewById(R.id.makepayment); 
     trackparcel = (Button) findViewById(R.id.usertrackbutton); 
     checkcard = (Button) findViewById(R.id.checkcard); 

     userinfo.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(MainMenu.this, UserInfo.class); 
       startActivity(intent); 
      } 
     }); 
     requestservice.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(MainMenu.this, RequestService.class); 
       startActivity(intent); 

      } 
     }); 
     makepayment.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(MainMenu.this, Payment.class); 
       startActivity(intent); 

      } 
     }); 
     trackparcel.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(MainMenu.this, TrackParcel.class); 
       startActivity(intent); 
      } 
     }); 
     checkcard.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent(MainMenu.this, CheckCard.class); 
       startActivity(intent); 
      } 
     }); 

    } 

    public void onBackPressed() { 
     new AlertDialog.Builder(MainMenu.this) 
       .setTitle("Logout") 
       .setMessage("Would you like to logout?") 
       .setPositiveButton("Yes", 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, 
            int which) { 
           Intent intent = new Intent(MainMenu.this, 
             Login.class); 
           intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           SharedPreferences sp = PreferenceManager 
             .getDefaultSharedPreferences(MainMenu.this); 
           Editor edit = sp.edit(); 
           edit.clear(); 
           edit.commit(); 
           startActivity(intent); 
           finish(); 

          } 
         }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // user doesn't want to logout 
        } 
       }).show(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main_menu, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.home: 
      // This ID represents the Home or Up button. In the case of this 
      // activity, the Up button is shown. Use NavUtils to allow users 
      // to navigate up one level in the application structure. For 
      // more details, see the Navigation pattern on Android Design: 
      // 
      // http://developer.android.com/design/patterns/navigation.html#up-vs-back 
      // 
      NavUtils.navigateUpFromSameTask(this); 
      return true; 

     case R.id.action_logout: { 
      new AlertDialog.Builder(MainMenu.this) 
        .setTitle("Logout") 
        .setMessage("Would you like to logout?") 
        .setPositiveButton("Yes", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) { 
            Intent intent = new Intent(MainMenu.this, 
              Login.class); 
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            SharedPreferences sp = PreferenceManager 
              .getDefaultSharedPreferences(MainMenu.this); 
            Editor edit = sp.edit(); 
            edit.clear(); 
            edit.commit(); 
            startActivity(intent); 
            finish(); 

           } 
          }) 
        .setNegativeButton("No", 
          new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, 
             int which) { 
            // user doesn't want to logout 
           } 
          }).show(); 
     } 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 

我想問的是那爲什麼我的動作項目不會出現在我的ActionBar事件中,儘管我已經寫過android:showAsAction="ifRoom|withText"android:showAsAction="always"我不知道爲什麼它的行爲如此。

回答

0

您正在擴展ActionbarActivity建議您使用支持庫中的Actionbar。

更改爲

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/action_settings" 
    android:logo="@drawable/ic_action_settings" 
    app:showAsAction="ifRoom|withText" 
    android:title="@string/action_settings"/> 

<item 
    android:id="@+id/action_logout" 
    android:logo="@drawable/logout" 
    app:showAsAction="ifRoom|withText" 
    android:title="@string/action_logout"/> 


</menu> 

引用文檔

注意,上面的showAsAction屬性使用在標籤定義的自定義名稱空間 。當使用由支持庫定義的任何XML 屬性時,這是必需的,因爲舊設備上的Android框架中不存在這些屬性的 。所以您必須使用您自己的名稱空間作爲 支持庫定義的所有屬性的前綴。

+0

哇這是偉大正確的代碼。但我希望圖標出現在actionBar而不是文本中 –

+0

@Jiazzyuser文檔中的另一個引用如果菜單項同時提供標題和圖標 - 標題和圖標屬性 - 則操作項僅顯示圖標默認。如果要顯示文本標題,請將「withText」添加到showAsAction屬性中。例如:'' – Raghunandan

+0

還注意到如果沒有足夠的空間它會出現在溢出菜單中 – Raghunandan

0

我在這裏做了愚蠢的錯誤.. 這是什麼,我已經改變了

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" > 

    <item 
     android:id="@+id/action_settings" 
     android:icon="@drawable/ic_action_settings" 
     yourapp:showAsAction="always" 
     android:title="@string/action_settings"/> 

    <item 
     android:id="@+id/action_logout" 
     android:icon="@drawable/logout" 
     yourapp:showAsAction="always" 
     android:title="@string/action_logout"/> 

</menu> 

變化android:logo="@drawable/ic_action_settings"android:icon="@drawable/ic_action_settings"

相關問題