如何分别监听 AppCompat 工具栏 logo/text 和后退导航项上的点击动作?
How to listen to click action on AppCompat's Toolbar logo/text and back navigation item separately?
我对此完全迷失了。我怎样才能做到这一点?
您可以使用工具栏:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tool_bar"
android:background="@color/light_blue"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout android:id="@+id/action_home"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/touch_selector"
android:clickable="true"
android:padding="@dimen/small">
<ImageView
android:id="@+id/home_icon"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="@drawable/white_square_icon"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/home_icon"
android:text="My App"
android:textColor="@color/white_color"
android:layout_marginLeft="@dimen/small"
android:textSize="18sp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
并在您的代码中:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.findViewById(R.id.action_home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// custom implementation
}
});
并实施 OnClick()
我对此完全迷失了。我怎样才能做到这一点?
您可以使用工具栏:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tool_bar"
android:background="@color/light_blue"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout android:id="@+id/action_home"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/touch_selector"
android:clickable="true"
android:padding="@dimen/small">
<ImageView
android:id="@+id/home_icon"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="@drawable/white_square_icon"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/home_icon"
android:text="My App"
android:textColor="@color/white_color"
android:layout_marginLeft="@dimen/small"
android:textSize="18sp" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
并在您的代码中:
Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
toolbar.findViewById(R.id.action_home).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// custom implementation
}
});
并实施 OnClick()