[英]No actions or icons showing on the actionbar; only in overflow
I'm not really new to programming but a complete fresh noob regarding android and java. 我不是真正的编程新手,而是关于android和java的全新手。
I recently made the decision to programm an Android App using this tutorial . 我最近决定使用此教程对Android App进行编程。 I got till the actionbar and can even display some actions like search and settings in the overflow. 我直到动作栏,甚至可以在溢出中显示一些动作,例如搜索和设置。 What I cant manage to do is to let an action appear on the actionbar(with or without icon). 我无法做的是让一个动作出现在动作栏上(带有或不带有图标)。
I managed to it once with this tutorial but I somehow cant repeat it on my actual "Tutorialapp". 我在本教程中做到了一次,但是我无法在我的实际“ Tutorialapp”上重复它。
Here is my code: 这是我的代码:
main.xml: main.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.newapp.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
<item
android:id="@+id/action_search"
android:orderInCategory="1"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="always"/>
</menu>
MainActivity.java: MainActivity.java:
package com.example.newapp;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.newapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void openSearch() {
Toast.makeText(this, "This is a Search Button", Toast.LENGTH_SHORT).show();
}
public void sendMessage (View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
try swapping the menu items: 尝试交换菜单项:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="always"/>
<item
android:id="@+id/action_settings"
android:orderInCategory="101"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>
I dont know why, but you need to specify in java code the action always. 我不知道为什么,但是您需要始终在Java代码中指定操作。 I had the same problem, and I solved it like that: 我有同样的问题,我这样解决了:
@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);
menu.findItem(R.id.actionSearch).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return true;
}
Hope it solve your problem. 希望它能解决您的问题。 :D!! :D! Good Luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.