我按照以下方式创建了弹出菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.shehabic.droppy_samples.MainActivity">
<group android:enabled="true">
<item
android:id="@+id/itemUserProfile"
android:icon="@drawable/popup_profile"
android:title="User Name"
>
</item>
<item
android:id="@+id/itemProfileSetting"
android:icon="@drawable/popup_setting"
android:title="Settings"
/>
</group>
<group android:enabled="true">
<item
android:id="@+id/itemFAQ"
android:icon="@drawable/popup_faq"
android:title="FAQ" />
<item
android:id="@+id/itemContactUs"
android:icon="@drawable/popup_contact_us"
android:title="Contact Us" />
</group>
<group android:enabled="true">
<item
android:id="@+id/itemAboutUs"
android:icon="@drawable/popup_about_us"
android:title="About Us" />
</group>
现在,在第一项中,我想显示一个在注册时捕获的图像。我将我的Image转换为base 64并存储在shared pref中。以及我做过的主要活动:
if(SharedPref.getSharedPref().getValue(getApplicationContext(),"name").length() >0){
user_name =SharedPref.getSharedPref().getValue(getApplicationContext(),"name");
}
if(SharedPref.getSharedPref().getValue(getApplicationContext(),"profile_img_encoded").length() > 0){
String encoded =SharedPref.getSharedPref().getValue(getApplicationContext(),"profile_img_encoded");
byte[] imageAsBytes = Base64.decode(encoded.getBytes(),Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
user_img = new BitmapDrawable(getResources(), bitmap);
}
但无法将其设置为菜单图标,因为user_img
属于android.graphics.drawable.Drawable
。有什么方法可以将我的图像显示为菜单图标,或者任何方式,以便我可以生成ListView
样式弹出菜单。
答案 0 :(得分:0)
Private Menu menu
@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);
this.menu = menu;
ImageView imageView = new ImageView(getActivity());
imageView.setMinimumHeight(128);
imageView.setMinimumWidth(128);
imageView.setImageDrawable(yourDrawable);
MenuItem item = this.menu.getItem(0);
item.setActionView(imageView);
return true;
}
第二个Approch:
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.action_settings);
// set your desired icon here based on a flag if you like
settingsItem.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
return super.onPrepareOptionsMenu(menu);
}
如果你想要带有图标的弹出菜单:
btnExpand.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(btnExpand.getContext(), itemView);
popup.setOnMenuItemClickListener(newPopupMenu.OnMenuItemClickListener(){
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_Delete:
return true;
case R.id.action_share:
return true;
default:
return false;
}
}
});
// here you can inflate your view
popup.inflate(R.menu.second_contect);
//popup.setGravity(Gravity.TOP);
// write this if you want menu with icons
try {
Field mFieldPopup=popup.getClass().getDeclaredField("mPopup");
mFieldPopup.setAccessible(true);
MenuPopupHelper mPopup = (MenuPopupHelper) mFieldPopup.get(popup);
mPopup.setForceShowIcon(true);
} catch (Exception e) {
}
popup.show();
}
});