有以下代码:
现在我想为这个图标设置点击监听器。这可行吗?提前感谢!
代码如下:
getActionBar().setIcon(R.drawable.basket);
现在我想为这个图标设置点击监听器。这可行吗?提前感谢!
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setHomeButtonEnabled(true);
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish(); // Or do you own task
return true;
}
return super.onOptionsItemSelected(item);
}
更多内容请参考为低级活动添加向上按钮。同时,添加操作栏是关于操作栏的一份很好的资源。
有两种方法可以制作自定义标题栏:
1.如果您是通过这种方式制作自定义标题的
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);//xml class of your mainActivity
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title_bar);
/////custom_title_bar is xml of which type you want to make your title bar
然后只需找到位于custom_title_bar中的小部件的ID,并执行与您在mainactivity xml中执行操作相同的操作。
就把它们视为您活动xml的小部件
值 -> menu.xml
<item
android:id="@+id/show"
android:showAsAction="always"
android:title="@string/show"
android:icon="@drawable/showbutn">
</item>
android:showAsAction="always"
这一行将项目放置在标题栏上。
然后在您的MainActivity类中重写onOptionsItemSelected方法:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.show:
///////perform your action what you want by clicking on item
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
就是这样....