我正在尝试将一个位于我的操作栏上的图像作为可点击的方式。我的意思是我想点击图片打开另一个课我该怎么办?
这是自定义操作栏,我以编程方式设置它。
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView imageView = new ImageView(actionBar.getThemedContext());
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(R.drawable.logoonactionbar);
ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT, Gravity.END | Gravity.CENTER_VERTICAL);
layoutParams.rightMargin = 20;
imageView.setLayoutParams(layoutParams);
actionBar.setCustomView(imageView);
actionBar.setTitle(getResources().getString(R.string.secondaryView));
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setIcon(android.R.color.transparent);
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.backgroundcolor)));
是否有可能,如果是这样,如何实现这一目标?
谢谢!
答案 0 :(得分:0)
实际上非常简单 只需在Menu.xml中添加项目即可 这是menu.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" />
使用此功能可以对这些按钮执行操作 这是功能代码
@Override public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch(); //Any function call or anything you want to perform
return true;
case R.id.action_settings:
openSettings(); //Any function call or anything you want to perform
return true;
default:
return super.onOptionsItemSelected(item);
} }
有关详细信息,请参阅开发人员指南 https://developer.android.com/training/basics/actionbar/adding-buttons.html