gpt4 book ai didi

java - Android:ActionBar(Android支持库)

转载 作者:行者123 更新时间:2023-12-01 23:18:38 25 4
gpt4 key购买 nike

我的 ActionBarCompat 有问题(来自支持库)。我不知道如何在 ActionBar 上添加一些按钮,我在 ActionBar 上画了黑色圆圈(在屏幕截图上)以及屏幕左侧的菜单。

拜托,我需要帮助!

enter image description here

屏幕左侧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);
}

也欢迎其他方式 =)谢谢你!新年快乐!

最佳答案

来自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>

将操作添加到 ActionBar

@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);
}
}

关于java - Android:ActionBar(Android支持库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20860382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com