这是一个带有可点击按钮的动作栏示例我尝试将其应用于其他活动但我未能将其代码变得非常简单但我无法看到他是如何做到的这就是我在谈论http://www.brucephillips.name/blog/index.cfm/2011/12/28/Android-App-Development--Using-The-ActionBar-Widget的示例有人知道他是怎么做到的吗?当我尝试它时,动作栏就失效了。 这是他在java中的代码
package name.brucephillips.actionbarexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class ActionBarExampleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Toast.makeText(this, "MenuItem " + item.getTitle() + " selected.", Toast.LENGTH_SHORT).show();
return true;
}
}
这是他的主菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/open" android:showAsAction="ifRoom|withText" android:title="Open" android:icon="@drawable/ic_action_open"></item>
<item android:id="@+id/save" android:showAsAction="ifRoom|withText" android:title="Save" android:icon="@drawable/ic_action_save"></item>
<item android:id="@+id/close" android:showAsAction="ifRoom|withText" android:title="Close" android:icon="@drawable/ic_action_close"></item>
<item android:id="@+id/history" android:showAsAction="ifRoom|withText" android:title="History" android:icon="@drawable/ic_action_history"></item>
<item android:id="@+id/settings" android:showAsAction="ifRoom|withText" android:title="Settings" android:icon="@drawable/ic_action_settings"></item>
</menu>
这是他的主要活动
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/introduction"/>
</LinearLayout>
答案 0 :(得分:1)
尝试这可能是您的设备没有适当的密度来显示菜单。
android:uiOptions="splitActionBarWhenNarrow"
将此信息放入您的清单中的活动代码中。
<强>更新: - 强>
菜单的完整代码将此代码原样放置....
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class AndroidMenusActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Initiating Menu XML file (menu.xml)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);
MenuItem item = menu.getItem(0);
String itemTitle = item.getTitle().toString();
Toast.makeText(this, "First menu item is: " + itemTitle, Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_bookmark:
Toast.makeText(this, "Bookmark is Selected",
Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(this, "Save is Selected",
Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(this, "Search is Selected",
Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(this, "Share is Selected",
Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_delete:
Toast.makeText(this, "Delete is Selected",
Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
}
// main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello" />
</LinearLayout>
// menu.xml它应该在res里面的菜单文件夹中
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/menu_bookmark"
android:showAsAction="ifRoom"
android:title="mark"/>
<item
android:id="@+id/menu_save"
android:showAsAction="ifRoom|withText"
android:title="Save"/>
<item
android:id="@+id/menu_search"
android:showAsAction="ifRoom"
android:title="Search"/>
<item
android:id="@+id/menu_share"
android:showAsAction="ifRoom"
android:title="Share"/>
<item
android:id="@+id/menu_delete"
android:showAsAction="ifRoom"
android:title="Delete"/>
</menu>
//清单
将uiOption放在像这样的活动中
<activity
android:name=".AndroidMenusActivity"
android:label="@string/app_name"
android:uiOptions="splitActionBarWhenNarrow" >