我正在尝试使用AppCompatActivity使app的徽标可点击。徽标位于工具栏的起始位置,并且左对齐。徽标显示正确,但徽标无法点击。我在这里缺少什么?
Activity.java:
Text::CSV::Encoded
toolbar.xml:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.ic_s_logo_48x48);
getSupportActionBar().setDisplayUseLogoEnabled(true);
}
@Override
public boolean onOptionsItemSelected (MenuItem item){
switch (item.getItemId()) {
case R.id.home:
Toast toast1 = Toast.makeText(MainActivity.this, "This is the homepage", Toast.LENGTH_LONG);
toast1.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast1.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:3)
您可以使用其他方式处理您的住所ImageButton
点击这样
ImageButton ivHome = (ImageButton) toolbar.findViewById(R.id.home);
ivHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log("TAG","Home image click");
}
});
<强> ====
OR 强>
您可以setNavigationIcon
使用Toolbar
(它将显示为您当前的主页按钮)
要做到这一点
1.像这样创建工具栏xml
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
2.更改Activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.your_home_icon));
3。点击
时处理事件toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("TAG", "home clicked");
}
});
希望这个帮助