我遇到ActionBarCompat问题(来自支持库)。 我不知道如何在ActionBar上添加几个按钮,我在屏幕左侧的菜单中绘制了黑色圆圈(截图)。
拜托,我需要帮助!
屏幕左侧ActionBar中的菜单代码。
private DrawerLayout mDrawerLayout;
private ListView mDrawer;
private ActionBarHelper mActionBar;
private ActionBarDrawerToggle mDrawerToggle;
...
linearLayout = (LinearLayout)findViewById(R.id.fragment_container);
linearLayout.setId(LAYOUT_ID);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer = (ListView) findViewById(R.id.left_drawer);
mDrawerLayout.setDrawerListener(new DDrawerListener());
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
...
fragments = new Fragment[NUMBER_OF_TABS];
mDrawer.setAdapter(new CustomAdapter(this));
mDrawer.setOnItemClickListener(new DrawerItemClickListener());
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab));
actionBar.setDisplayHomeAsUpEnabled(true);
mActionBar = createActionBarHelper();
mActionBar.init();
mDrawer.setBackgroundColor(Color.parseColor("#e5c391"));
mDrawer.setCacheColorHint(Color.parseColor("#e5c391"));
initArrays(this);
mDrawerToggle = new ActionBarDrawerToggle
(this, mDrawerLayout,R.drawable.ic_drawer,R.string.app_drawer_open, R.string.app_drawer_close);
if (savedInstanceState == null)
{
addFragment(0);
}
欢迎其他方式=) 谢谢! 新年快乐!
答案 0 :(得分:2)
来自https://developer.android.com/training/basics/actionbar/adding-buttons.html:
以XML格式指定操作
动作溢出中可用的所有操作按钮和其他项目都在XML菜单资源中定义。要向操作栏添加操作,请在项目的res / menu /目录中创建一个新的XML文件。
为要包含在操作栏中的每个项目添加元素。例如:
res/menu/main_activity_actions.xml
<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>
将操作添加到操作栏
@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 presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 1 :(得分:1)
请参阅Android Developer门户网站中的note。