如何将下拉菜单设为下图?
我的应用中有+ button
。点击后,我希望它显示在+ button
下的下拉菜单。
这是我到目前为止所尝试过的。单击+ button
时,不会显示下拉菜单。
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button addNew; // for + button
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addNew=(Button)findViewById(R.id.add);
addNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu po=new PopupMenu(MainActivity.this,addNew); //for drop-down menu
po.getMenuInflater().inflate(R.menu.popup_menu,po.getMenu());
po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
public boolean onMenuItemClick(MenuItem item)
{
return true;
}
});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.create_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch(item.getItemId()){
case R.id.add:
}
return super.onOptionsItemSelected(item);
}
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/addForm"
android:title="Create New "/>
<item
android:id="@+id/editForm"
android:title="Edit File"/>
</menu>
有人可以给我一些提示吗?谢谢
答案 0 :(得分:1)
您尚未将PopMenu的可见性设置为可见
使用
po.show();