在 Android 上向 ActionBar 添加图标
Adding icons to the ActionBar on Android
我正在学习下一个教程:Adding Action Buttoms,我是一步步完成的,但是当我 运行 我的应用程序没有显示任何图标时。我正在调试,但一切似乎都是正确的,并且我有任何 nullPointerException 或类似的东西。
这是我的主要活动:
package com.training.training.appone;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = " com. training.example.appone.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_my, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activiy_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.txt_edit_message);
String message = editText.getText().toString();
// El primer parametro es el KeyName:
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
我把这个 XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
我在最后一行有一个错误 "android:showsAsAction="never",我在我的构建中下载并引用了 libray gradle:编译 'com.android.support:appcompat-v7:22.0.0' 我也尝试更改 "android" 到 "res-auto",但它说项目必须有一个标题(他们有):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
我的图书馆:
在这一点上我不知道发生了什么。
可能因为您正在使用 AppCompat 库,所以您应该使用
app:showAsAction="never"
改为
android:showAsAction="never"
在 menu.xml
中进行以下更改
<menu
....
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
...
app:showAsAction="never" />
</menu>
如果你想在 actionBar 上显示你的菜单,那么它应该是 app:showAsAction="always"
。
在您的 XML 文件中,请更改此行
app:showAsAction="always"
而不是
android:showAsAction="never"
如果您查看教程中的 xml 模式,您将同时拥有:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
注意 xmlns:android 和 xmlns:yourapp 是不同的.所以你需要做的就是:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
yourapp:showAsAction="never" />
</menu>
android 和 yourapp 标签可以任意命名。它们只是对架构的命名引用。
我正在学习下一个教程:Adding Action Buttoms,我是一步步完成的,但是当我 运行 我的应用程序没有显示任何图标时。我正在调试,但一切似乎都是正确的,并且我有任何 nullPointerException 或类似的东西。
这是我的主要活动:
package com.training.training.appone;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = " com. training.example.appone.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_my, menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activiy_actions, menu);
return super.onCreateOptionsMenu(menu);
}
@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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view){
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.txt_edit_message);
String message = editText.getText().toString();
// El primer parametro es el KeyName:
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
我把这个 XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
我在最后一行有一个错误 "android:showsAsAction="never",我在我的构建中下载并引用了 libray gradle:编译 'com.android.support:appcompat-v7:22.0.0' 我也尝试更改 "android" 到 "res-auto",但它说项目必须有一个标题(他们有):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
我的图书馆:
在这一点上我不知道发生了什么。
可能因为您正在使用 AppCompat 库,所以您应该使用
app:showAsAction="never"
改为
android:showAsAction="never"
在 menu.xml
中进行以下更改<menu
....
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
...
app:showAsAction="never" />
</menu>
如果你想在 actionBar 上显示你的菜单,那么它应该是 app:showAsAction="always"
。
在您的 XML 文件中,请更改此行
app:showAsAction="always"
而不是
android:showAsAction="never"
如果您查看教程中的 xml 模式,您将同时拥有:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto"
注意 xmlns:android 和 xmlns:yourapp 是不同的.所以你需要做的就是:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
yourapp:showAsAction="never" />
</menu>
android 和 yourapp 标签可以任意命名。它们只是对架构的命名引用。