我一直在开发Android应用程序,我有菜单项。但是我的问题是,当我在三星Galaxy S4上测试时,它不在右上角,但如果我使用Experia进行测试(尺寸比三星更短),它可以显示在右上角。怎么处理?只显示第一项" Pencarian"有图标,但其他人没有出现。
我的菜单:
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menusearch"
android:icon="@drawable/searchicon"
android:title="Pencarian"
android:showAsAction="ifRoom|collapseActionView"
android:actionLayout="@layout/search_layout"/>
<item android:id="@+id/menuadvancedsearch"
android:icon="@drawable/searchicon2"
android:title="Pencarian Advanced"
android:showAsAction="collapseActionView"/> <!-- pilihan default collpasactionview -->
<item android:id="@+id/menulogin"
android:icon="@drawable/usericon"
android:title="Login"
android:showAsAction="collapseActionView"/>
</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.tab_sport, menu);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@SuppressLint("NewApi")
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.menusearch:
//Toast.makeText(getBaseContext(), "test", Toast.LENGTH_LONG).show();
View view = item.getActionView();
final EditText edtsearchitem=(EditText)view.findViewById(R.id.edtsearchitem);
edtsearchitem.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
//Global.MainActivity.finish();
OtherClass.setKeyword(edtsearchitem.getText().toString());
OtherClass.setFlagSearch(true);
Intent MyIntentDetailItem=new Intent(getBaseContext(), SearchActivity.class);
startActivity(MyIntentDetailItem);
return true;
}
return false;
}
});
ImageView imgcross=(ImageView)view.findViewById(R.id.imgcross);
imgcross.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
edtsearchitem.setText("");
}
});
return true;
case R.id.menuadvancedsearch:
OtherClass.setFlagSearch(false);
Intent MyIntentDetailItem=new Intent(getBaseContext(), SearchActivity.class);
startActivity(MyIntentDetailItem);
return true;
case R.id.menulogin:
LayoutInflater li = LayoutInflater.from(MainActivity.this);
final View inputdialogcustom = li.inflate(R.layout.custom_user, null);
final AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setView(inputdialogcustom);
final EditText edtuser=(EditText)inputdialogcustom.findViewById(R.id.edtuser);
final EditText edtpassword=(EditText)inputdialogcustom.findViewById(R.id.edtpassword);
Button btnlogin=(Button)inputdialogcustom.findViewById(R.id.btnlogin);
Button btncancel=(Button)inputdialogcustom.findViewById(R.id.btncancel);
btnlogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
connection = new ViewConnection(getBaseContext());
if (connection.isConnectingToInternet())
{
BaseUrl=OtherClass.URLlogin+"?user="+edtuser.getText().toString()+
"&pass="+edtpassword.getText().toString();
executeAsyncTask();
}else {
// Internet connection is not present
/*LayoutInflater li = LayoutInflater.from(MainActivity.this);
final View inputdialogcustom = li.inflate(R.layout.activity_confirm_connection, null);
AlertDialog.Builder alerterror = new AlertDialog.Builder(MainActivity.this);
alerterror.setView(inputdialogcustom);
alerterror.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
alerterror.show();*/
Toast.makeText(getBaseContext(), "Tidak ada koneksi internet", Toast.LENGTH_SHORT).show();
}
}
});
btncancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
alert.dismiss();
}
});
alert.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
你不能这样做,或者至少你不应该这样做。菜单的位置是特定于设备的。大多数三星设备右上角没有溢出菜单,而是底部有一个物理菜单按钮。自Android 3.0(Honeycomb)以来,这些按钮已被弃用,但一些OEM仍然选择使用它们。在具有此类物理菜单按钮的设备上,ActionBar
默认返回到Honeycomb之前的行为,并且不显示溢出菜单。
总结一下:
菜单按钮的位置是设备功能。你是一个 开发者对此没有影响力。您只需定义菜单项, 但设备决定它们是否显示在溢出菜单中 不