2013-02-21 44 views
13

Cách hiển thị biểu tượng với menu tùy chọn.Tôi đã thử mã sau nhưng menu tùy chọn của tôi không có biểu tượng hình ảnh. Tôi đang sử dụng phiên bản Android 4.0 để phát triển ứng dụng.menu tùy chọn Android có biểu tượng

mã Java:

public boolean onCreateOptionsMenu(Menu menu) { 
      super.onCreateOptionsMenu(menu); 
      menu.add("Add Contacts").setIcon(
        R.drawable.ic_launcher); 

      return true; 
     } 

Tiếp theo là màn hình ứng dụng của tôi bắn

enter image description here

tôi cần hình ảnh sẽ được hiển thị trên đỉnh của "Add Contacts" mục.

+0

nó một câu hỏi hay. Quá xấu nó không có câu trả lời. Không có phương pháp nào trong số này hoạt động. –

Trả lời

3

Bạn có thể tạo một menu tùy chỉnh như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/add_contacts" 
      android:icon="@drawable/ic_launcher" 
      android:title="@string/add_contacts" 
     /> 
</menu> 

Và sau đó thổi phồng nó

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.my_menu, menu); 
    return true; 
} 

Thông tin thêm về điều này ở đây: http://developer.android.com/guide/topics/ui/menus.html#options-menu

+1

tôi đã thử điều này nhưng hình ảnh không đến. – sonia

+0

http://stackoverflow.com/questions/8306943/android-menu-icons-are-not-displaying-when-the-api-level-is-above-10 – nedaRM

3

bạn có thể trực tiếp đặt này vào xml tập tin.

<item android:id="@+id/add_contacts" 
    android:icon="@android:drawable/plus_icon" 
    android:title="Add Contacts"/> 
1

Bạn có thể thử theo dõi Link này.

Kiểm tra điều này và cho tôi biết liệu nó có hoạt động hay không.

Hoặc bạn có thể làm một số việc như thế này.
Tạo menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:id="@+id/next" 
       android:icon="@drawable/ic_next" 
       android:title="@string/next" /> 
     <item android:id="@+id/previous" 
      android:icon="@drawable/ic_previous" 
      android:title="@string/previous" /> 
     <item android:id="@+id/list" 
      android:icon="@drawable/ic_list" 
      android:title="@string/list" /> 
</menu> 

Và bây giờ bạn sẽ có thể thiết lập ICON trên menu

Bây giờ trong CreateOptionMenu

public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     return true; 
    } 

Và để truy cập menu.

public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
     case R.id.next: 
      Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option", 
         Toast.LENGTH_SHORT).show(); 
      return true; 
     … 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 
+2

một lần nữa biểu tượng tương tự không hiển thị. – sonia

+0

Ohk sau đó bạn có thể thử bằng cách tạo tệp menu.xml và bạn có thể đặt các mục trong đó và cũng thiết lập ICON trong đó. –

+0

Kiểm tra mã tôi đã chỉnh sửa câu trả lời. Nếu nó hoạt động hoặc không cho tôi biết. –

1

Nếu bạn sử dụng một số thuộc tính sau trong file manifest sau đó nó sẽ được hiển thị biểu tượng của bạn ....

<activity android:name=".ui.CategoryActivity" 
     android:label="@string/app_name" 
     **android:theme="@android:style/Theme.NoTitleBar"**></activity> 

Đó là làm việc tốt cho tôi ... :) 1 cho riêng tôi nỗ lực ...

** phải được nhập.

3

Override OnPrepareOptionsMenu và thêm biểu tượng từ đó quá

và nếu nó cho trên 3.0, sử dụng android:showAsAction trong xml.

ví dụ: android:showAsAction="ifRoom|withText"

0

sự cố là tệp Androidmanifest.xml. Di android:theme="@style/AppTheme" và nó sẽ chỉ làm việc tốt

2

Tôi đã thử các mã trong hai dòng và nó hoạt động:

public boolean onCreateOptionsMenu(Menu menu) { 
     super.onCreateOptionsMenu(menu); 
     menu.add("Add Contacts"); 
     menu.getItem(0).setIcon(R.drawable.ic_launcher); 
     return true; 
} 
Các vấn đề liên quan