我正在关注Docant pdf book中的官方android教程。出于某种原因,我的搜索图标不会显示出来。我收录了来自holo_dark的ic_action_search.png照片,以及使用带有暗动作条主题的全息灯。 这是代码,eclipse找不到任何错误,min sdk设置为11。
DisplayMessageActivity.java文件
package com.example.myfirstapp1;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<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="always" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
答案 0 :(得分:0)
我认为您的错误位于onCreate method
,在此行中:
setContentView(textView);
您应该将内容视图设置为视图的xml,而不是文本视图。
你的onCreate应该是这样的:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(the_name_of_your_xml_for_this_activity);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
}
答案 1 :(得分:0)
我建议您使用操作栏库http://actionbarsherlock.com/ 为了在任何与操作栏相关的操作上获得更好的结果
答案 2 :(得分:0)
我认为你需要先在res / valuse / drawables.xml中定义它
<item name="ic_action_search" type="drawable">@android:drawable/ic_menu_search</item>
检查照片,
如果你有.bng照片,你必须通过另一种方式导入它而不是使用
android:src="@drawable/ic_action_search"