我怎样才能复制下面在Balsamiq中制作的内容?
我制作了这个菜单,但它只显示了项目的文本(而不是图标)。是否可以在PopupMenu中显示标题和图标?
这是我的create_post_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_photo"
android:icon="@drawable/ic_action_camera"
android:title="@string/action_photo"
android:showAsAction="always|withText" />
<item
android:id="@+id/action_video"
android:icon="@drawable/ic_action_video"
android:title="@string/action_video"
android:showAsAction="always|withText" />
<item
android:id="@+id/action_text"
android:icon="@drawable/ic_action_edit"
android:title="@string/action_text"
android:showAsAction="always|withText" />
<item
android:id="@+id/action_link"
android:icon="@drawable/ic_action_web_site"
android:title="@string/action_link"
android:showAsAction="always|withText" />
</menu>
修改
以下是我的onCreateOptionsMenu
和onOptionsItemSelected
方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_new) {
View menuItemView = findViewById(R.id.action_new);
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.create_post_menu);
popupMenu.show();
return true;
} else if(item.getItemId() == R.id.action_search) {
return true;
} else if(item.getItemId() == R.id.action_settings) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return true;
} else if(item.getItemId() == R.id.action_help) {
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:15)
我通过简单地将create_post_menu
放在图标为item
的{{1}}内解决了这个问题。
例如,我(使用AppCompat):
+
如果没有AppCompat,您可以通过将<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_new"
android:icon="@drawable/ic_action_new"
android:title="@string/action_new"
app:showAsAction="always">
<menu>
<item
android:id="@+id/action_photo"
android:icon="@drawable/ic_action_camera"
android:title="@string/action_photo"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_video"
android:icon="@drawable/ic_action_video"
android:title="@string/action_video"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_text"
android:icon="@drawable/ic_action_text"
android:title="@string/action_text"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_place"
android:icon="@drawable/ic_action_place"
android:title="@string/action_place"
app:showAsAction="always|withText" />
<item
android:id="@+id/action_more"
android:title="@string/action_more"
android:visible="false"
app:showAsAction="always|withText" />
</menu>
</item>
...(more menu items here)
</menu>
替换为app
来摆脱XML命名空间app
。
答案 1 :(得分:12)
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);//your created butto
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.popup_menu, popup.getMenu());
//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
}
}
答案 2 :(得分:0)
我希望我之前的回答Here能为您提供帮助
如果您只想要一个类似的弹出菜单,可以使用ActionProvider。它更强大。
如果您希望它作为真正的菜单,您可以使用自定义PopupMenu。