问题描述
我不是真正的新节目,但一个完整的新鲜小白关于机器人和Java。
我最近作出的决定将程序的Android应用程序使用此。
我直到动作条,甚至可以显示像溢出搜索和设置一些行动。我不能设法要做的就是让一个动作出现在动作条(带或不带图标)。
我这个但我有点斜面重复它在我实际的Tutorialapp。
下面是我的code:
main.xml中:
<?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
工具:上下文=com.example.newapp.MainActivity><项目
机器人:ID =@ + ID / action_settings
机器人:orderInCategory =100
机器人:标题=@字符串/ action_settings
应用:showAsAction =从不/>
<项目
机器人:ID =@ + ID / ACTION_SEARCH
机器人:orderInCategory =1
机器人:图标=@绘制/ ic_action_search
机器人:标题=@字符串/ ACTION_SEARCH
机器人:showAsAction =总是/>
&所述; /菜单>
MainActivity.java:
包com.example.newapp;进口android.support.v7.app.ActionBarActivity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.EditText;
进口android.widget.Toast;公共类MainActivity扩展ActionBarActivity {公共最后静态字符串EXTRA_MESSAGE =com.example.newapp.MESSAGE;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){ //充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
INT ID = item.getItemId();
如果(ID == R.id.action_settings){
返回true;
} 开关(item.getItemId()){ 案例R.id.action_search:
OpenSearch的();
返回true; 默认:
返回super.onOptionsItemSelected(项目);
}
}私人无效的OpenSearch(){
Toast.makeText(这一点,这是一个搜索按钮,Toast.LENGTH_SHORT).show();}公共无效的sendMessage(查看视图){
意向意图=新意图(这一点,DisplayMessageActivity.class);
EDITTEXT的EditText =(EditText上)findViewById(R.id.edit_message);
字符串消息= editText.getText()的toString()。
intent.putExtra(EXTRA_MESSAGE,消息);
startActivity(意向);
}
}
请尝试换菜单项:
<?XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android><项目
机器人:ID =@ + ID / ACTION_SEARCH
机器人:图标=@绘制/ ic_action_search
机器人:标题=@字符串/ ACTION_SEARCH
机器人:showAsAction =总是/>
<项目
机器人:ID =@ + ID / action_settings
机器人:orderInCategory =101
机器人:标题=@字符串/ action_settings
应用:showAsAction =从不/>
&所述; /菜单>
I'm not really new to programming but a complete fresh noob regarding android and java.
I recently made the decision to programm an Android App using this tutorial.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".
Here is my code:
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:
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>
这篇关于没有动作或图标显示在动作条;只有在溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!