2014-04-17 2 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"/> 

이미지가 안드로이드의 팝업 메뉴에 나타나지 않습니까?

<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 제대로 작동하지만 안드로이드 : icon = "@ drawable/ic_launcher"는 작동하지 않습니다. popmenu에 이미지/아이콘이 나타나지 않습니다. 어떻게 안드로이드의 팝업 메뉴에 아이콘을 설정할 수 있습니까? 나는 안드로이드에 새로운입니다.

+0

설정 showAsAction = "ifRoom". –

+0

선생님, showAsAction = "ifRoom"이 팝업 메뉴가 아닌 제목 오른쪽에 이미지를 표시하고 있습니다. –

+0

이 답변을 확인하십시오 - http://stackoverflow.com/a/20836454/1552622 – makovkastar

답변

1

변경 라인 :

android:showAsAction="never" 

android:showAsAction="ifRoom" 

또는

android:showAsAction="always" 
+0

해당 제목 아이콘이 팝업 메뉴에 없습니다. 내 팝업 메뉴는 버튼의 클릭을 –

+0

클릭하면 해당 팝업 메뉴에 대해 다른 XML 파일을 만들고 위 라인을 설정해야합니다. – Riser

+0

선생님, 나는 /res/menu/main.xml에서 그 파일을 만들고 있지만 작동하지 않습니다. 컨텍스트 메뉴 또는 기타와 같은 팝업 메뉴에 문제가 있음을 알고 싶습니다. –

관련 문제