2011-06-22 40 views
42

Ở đây tôi đã cố gắng để làm cho trình đơn tùy chọn, nhưng thực đơn không được hiển thị trên màn hình, vì vậy xin vui lòng hướng dẫn cho tôi nơi tôi làm sai lầm ...Android, Làm thế nào để tạo ra tùy chọn menu

MenuTest.java

public class MenuTest extends Activity { 
    @Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater=getMenuInflater(); 
    inflater.inflate(R.menu.more_tab_menu, menu); 
    return super.onCreateOptionsMenu(menu); 

} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) 
    { 
    case R.id.feeds: 
     break; 
    case R.id.friends: 
     break; 
    case R.id.about: 
     break; 
    } 
    return true; 
} 
} 

Và tập tin XML của tôi là more_tab_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<item 
    android:id="@+id/feeds" 
    android:title="Feeds"/> 
<item 
    android:id="@+id/friends" 
    android:title="Friends"/> 
<item 
    android:id="@+id/about" 
    android:title="About"/> 
</menu> 

hãy hướng dẫn cho tôi,

+0

Đây có phải là mã hoạt động hoàn chỉnh không? Phương thức onCreate ở đâu? – Egor

+0

tôi cũng đã viết phương pháp onCreate, nhưng sau đó tôi không nhận được menu trong màn hình. –

+0

bạn có nhấn 'nút menu' từ trình giả lập không? – Android

Trả lời

38
public class MenuTest extends Activity { 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.more_tab_menu, menu); 

     // return true so that the menu pop up is opened 
     return true; 
    } 
} 

và đừng quên nhấn nút menu hoặc biểu tượng trên Emulator hoặc thiết bị

10

Thay đổicủa bạnphương thức trả lại true. Để báo giá the docs:

Bạn phải trả về true cho menu được hiển thị; nếu bạn trả về false nó sẽ không được hiển thị.

26

xin vui lòng xem: ==

private int group1Id = 1; 

int homeId = Menu.FIRST; 
int profileId = Menu.FIRST +1; 
int searchId = Menu.FIRST +2; 
int dealsId = Menu.FIRST +3; 
int helpId = Menu.FIRST +4; 
int contactusId = Menu.FIRST +5; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    menu.add(group1Id, homeId, homeId, "").setIcon(R.drawable.home_menu); 
    menu.add(group1Id, profileId, profileId, "").setIcon(R.drawable.profile_menu); 
    menu.add(group1Id, searchId, searchId, "").setIcon(R.drawable.search_menu); 
    menu.add(group1Id, dealsId, dealsId, "").setIcon(R.drawable.deals_menu); 
    menu.add(group1Id, helpId, helpId, "").setIcon(R.drawable.help_menu); 
    menu.add(group1Id, contactusId, contactusId, "").setIcon(R.drawable.contactus_menu); 

    return super.onCreateOptionsMenu(menu); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case 1: 
     // write your code here 
     Toast msg = Toast.makeText(MainHomeScreen.this, "Menu 1", Toast.LENGTH_LONG); 
     msg.show(); 
     return true; 

    case 2: 
     // write your code here 
     return true; 

    case 3: 
     // write your code here 
     return true; 

    case 4: 
     // write your code here 
     return true; 

    case 5: 
     // write your code here 
     return true; 

    case 6: 
     // write your code here 
     return true; 

    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 
+2

Biểu tượng trình đơn không hiển thị:/ – Zame

2

Thay trở super.onCreateOptionsMenu (menu) ; với trả về true; trong phương pháp onCreateOptionsMenu bạn Điều này sẽ giúp

Và bạn cũng cần phải có phương pháp onCreate trong hoạt động của bạn

7
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    new MenuInflater(this).inflate(R.menu.folderview_options, menu); 
    return (super.onCreateOptionsMenu(menu)); 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    if (item.getItemId() == R.id.locationListRefreshLocations) { 
     Cursor temp = helper.getEmployee(active_employeeId); 
     String[] matches = new String[1]; 
     if (temp.moveToFirst()) { 
      matches[0] = helper.getEmployerID(temp); 
     } 
     temp.close();    
     startRosterReceiveBackgroundTask(matches); 
    } else if (item.getItemId()==R.id.locationListPrefs) { 
     startActivity(new Intent(this, PreferencesUnlockScreen.class)); 
     return true; 
    }   
    return super.onOptionsItemSelected(item); 
} 
+0

tùy chọn ẩn với mã. Bên trong onCreateOptionsMenu (Menu Menu), tôi đã thay đổi trả về (super.onCreateOptionsMenu (menu)) để trả về giá trị true mà làm cho nó hiển thị. – Dexter

0

Những câu trả lời trước đó đã đề cập menu truyền thống được sử dụng trong Android. Họ là một tùy chọn mà bạn có thể sử dụng nếu bạn đang tìm kiếm một sự thay thế

https://github.com/AnshulBansal/Android-Pulley-Menu

đơn Pulley là một thay thế cho Menu truyền thống cho phép người dùng lựa chọn bất kỳ tùy chọn cho một hoạt động bằng trực giác. Trình đơn được tiết lộ bằng cách kéo màn hình xuống dưới và trong người dùng cử chỉ đó cũng có thể chọn bất kỳ tùy chọn nào.

0

Lập trình giao diện người dùng Android hơi phức tạp một chút. Để bật menu Tùy chọn, ngoài mã bạn đã viết, chúng tôi cũng cần gọi hàm setHasOptionsMenu (true) trong phương thức OnCreate() của bạn. Hy vọng điều này sẽ giúp bạn.

2
import android.app.Activity; 
import android.os.Bundle; 
import android.view.*; 
import android.widget.*; 

public class AndroidWalkthroughApp2 extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // show menu when menu button is pressed 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.options_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // display a message when a button was pressed 
     String message = ""; 
     if (item.getItemId() == R.id.option1) { 
      message = "You selected option 1!"; 
     } 
     else if (item.getItemId() == R.id.option2) { 
      message = "You selected option 2!"; 
     } 
     else { 
      message = "Why would you select that!?"; 
     } 

     // show message via toast 
     Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG); 
     toast.show(); 

     return true; 
    } 
} 
0

Nếu thiết bị đang chạy v.4.1.2 Android hoặc trước đó,
menu không được hiển thị trong hành động-bar.
Nhưng nó có thể được truy cập thông qua Menu- (phần cứng) -Button.

0

Good Day Tôi đã kiểm tra Và nếu Bạn chọn Rỗng Activity Bạn không hề có xây dựng trong các chức năng menu Đối với xây dựng trong Bạn phải chọn Basic Activity Bằng cách này Bạn Activity sẽ chạy onCreateOptionsMenu

Hoặc nếu bạn làm việc trong Empty Activity từ đầu Chenge trong styles.xml các

Các vấn đề liên quan