2014-04-17 62 views
-2
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 

<item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:icon="@drawable/ic_launcher" 
    android:title="@string/action_settings"/> 

圖像不出現在android的popmenu?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/hello_world" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="62dp" 
    android:layout_marginTop="50dp" 
    android:text="Show Popup" /> 

//

/res/layout/activity_main.xml

public class MainActivity extends Activity { 
private Button button1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      button1 = (Button) findViewById(R.id.button1); 
      button1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
      //Creating the instance of PopupMenu 




      PopupMenu popup = new PopupMenu(MainActivity.this, button1); 
      //Inflating the Popup using xml file 
      popup.getMenuInflater().inflate(R.menu.main, popup.getMenu()); 
      //popup.add(0, MENU_QUIT, 0, "Quit").setIcon(R.drawable.menu_quit_icon); 

      //registering popup with OnMenuItemClickListener 

      popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 

      public boolean onMenuItemClick(MenuItem item) { 

       Toast.makeText(MainActivity.this,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show(); 
       return true; 
      } 
      }); 

      popup.show();//showing popup menu 
      } 
      });//closing the setOnClickListener method 
     } 


@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); 
    return true; 
} 

}

//這是主要的活動

p opup正常工作,但android:icon =「@ drawable/ic_launcher」不工作沒有圖像/圖標出現在popmenu中,請告訴我如何在android中的popmenu中設置圖標。 我是Android新手。

+0

組showAsAction = 「ifRoom」。 –

+0

sir,set showAsAction =「ifRoom」在標題上顯示的圖像不在popup菜單 –

+0

請檢查此答案 - http://stackoverflow.com/a/20836454/1552622 – makovkastar

回答

1

更改行:

android:showAsAction="never" 

android:showAsAction="ifRoom" 

android:showAsAction="always" 
+0

sir它在標題欄上顯示圖標但不在popup菜單中,我的popupmenu按鈕的點擊工作 –

+0

然後你必須爲該popupmenu做另一個XML文件,並設置上述行。 – Riser

+0

先生,我在/res/menu/main.xml中創建該文件,但它也不起作用。我想知道,像上下文菜單等popmenu有任何問題... –