Android: 主页标志不可点击?
Android: Home logo not clickable?
我正在尝试使用 AppCompatActivity 使应用程序的徽标可点击。徽标位于工具栏的起始位置并左对齐。徽标显示正确,但徽标不可单击。我在这里错过了什么?
Activity.java:
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);
}
}
toolbar.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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home"
android:title="@string/action_logo"
android:contentDescription="@string/action_logo"
android:icon="@drawable/ic_s_logo_48x48"
android:gravity="start"
android:clickable="true"
app:showAsAction="always" />
</android.support.v7.widget.Toolbar>
你可以用另一种方式来处理你的家ImageButton
clicked like this
ImageButton ivHome = (ImageButton) toolbar.findViewById(R.id.home);
ivHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log("TAG","Home image click");
}
});
====
或
您可以为您的 Toolbar
使用 setNavigationIcon
(它会像您当前的主页按钮一样显示)
为此
1.Create 工具栏 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.Change 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");
}
});
希望对您有所帮助
我正在尝试使用 AppCompatActivity 使应用程序的徽标可点击。徽标位于工具栏的起始位置并左对齐。徽标显示正确,但徽标不可单击。我在这里错过了什么?
Activity.java:
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);
}
}
toolbar.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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home"
android:title="@string/action_logo"
android:contentDescription="@string/action_logo"
android:icon="@drawable/ic_s_logo_48x48"
android:gravity="start"
android:clickable="true"
app:showAsAction="always" />
</android.support.v7.widget.Toolbar>
你可以用另一种方式来处理你的家ImageButton
clicked like this
ImageButton ivHome = (ImageButton) toolbar.findViewById(R.id.home);
ivHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log("TAG","Home image click");
}
});
====
或
您可以为您的 Toolbar
使用 setNavigationIcon
(它会像您当前的主页按钮一样显示)
为此
1.Create 工具栏 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.Change 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");
}
});
希望对您有所帮助