我想点击操作栏。
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(null);
getSupportActionBar().setLogo(R.drawable.ic_launcher);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
我尝试了这段代码,但没有成功: -
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch(item.getItemId()){
case R.id.action_search:
Intent search = new Intent(MainActivity.this,SearchActivity.class);
startActivity(search);
return true;
case R.id.login:
if(session.checkLogin())
finish();
else {
Intent intent = new Intent(MainActivity.this,MyAccount.class);
startActivity(intent);
}
return true;
case R.id.my_order:
alert.showAlertDialog(MainActivity.this,"Alert","This Page Under Maintenance",false);
return true;
case R.id.wishlist:
if(session.checkLogin()){
finish();
}
else {
Intent wishlistIntent = new Intent(MainActivity.this,WishList.class);
startActivity(wishlistIntent);
}
return true;
case android.R.id.home:
Intent intent = new Intent(MainActivity.this,MainActivity.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
任何人都可以帮我制作可点击的logo.I我是Android开发的新手请帮助我。提前致谢!
答案 0 :(得分:1)
工具栏基本上只是一个ViewGroup,因此您可以向其添加TextView并收听类似的onClick事件。
Add TextView to Toolbar in XML:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/action_bar_bkgnd"
app:theme="@style/ToolBarTheme" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:id="@+id/toolbar_title" />
</android.support.v7.widget.Toolbar>
在您的活动中收听点击次数:
toolbarTop.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"Clicked");
}
});
答案 1 :(得分:1)
Toolbar
类动态生成其子View
,因此默认情况下它们都没有资源ID。此外,该课程不会以任何我能找到的方式公开展示徽标View
。但是,我们可以稍微反思一下View
的引用。
private void setLogoOnClickListener(Toolbar toolbar, OnClickListener listener) {
try {
Class<?> toolbarClass = Toolbar.class;
Field logoField = toolbarClass.getDeclaredField("mLogoView");
logoField.setAccessible(true);
ImageView logoView = (ImageView) logoField.get(toolbar);
if(logoView != null) {
logoView.setOnClickListener(listener);
}
}
catch (NoSuchFieldException |
IllegalAccessException e) {
e.printStackTrace();
}
}
在Toolbar
上设置徽标后,必须调用此方法。例如:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.drawable.ic_launcher);
setLogoOnClickListener(toolbar, new OnClickListener() {
@Override
public void onClick(View p1) {
Toast.makeText(MainActivity.this, "Logo clicked", 0).show();
}
}
);
答案 2 :(得分:0)
这是我的解决方案:
Toolbar toolbar = ...
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
Drawable logo = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
getSupportActionBar().setIcon(logo);
for (int i = 0; i < toolbar.getChildCount(); i++) {
if (toolbar.getChildAt(i) instanceof ImageView) {
ImageView maybeLogo = ((ImageView) toolbar.getChildAt(i));
if (maybeLogo.getDrawable() == logo) {
maybeLogo.setOnClickListener(v -> mDrawerLayout.openDrawer(Gravity.START));
break;
}
}