为工具栏上的图标添加 onclick 监听器

Add onclick listener to icons on toolbar

[我的工具栏][1]

有人有解决办法吗?

您需要检查 R.id.img_bar_back。无论是您找到的 ImageView id 还是其他东西。这可能是因为找到错误的视图 ID。

 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/tool_bar_color"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/toolbar_iv_image1"
                    android:layout_width="?actionBarSize"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:text="@string/e_wallet"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/font_20sp" />

                <ImageButton
                    android:id="@+id/iv_invite"
                    android:layout_width="@dimen/padding_40dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="?selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:src="@drawable/invite_whit_icon" />

                <ImageButton
                    android:id="@+id/iv_notifications"
                    android:layout_width="?actionBarSize"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="?selectableItemBackgroundBorderless"
                    android:clickable="true"
                    android:src="@drawable/notifiction_white" />
            </LinearLayout>

        </android.support.v7.widget.Toolbar>

我看过你的工具栏。 这是我的解决方案:

  • 首先创建一个菜单xml资源文件。(这将包含你的"heart icon",res/menu/menu_example。xml)

随便取个名字吧,举个例子我就取个名字menu_example.

menu_example.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
       android:id="@+id/action_addfav"
       android:title=""
       app:showAsAction="always"
       android:icon="@drawable/YOUR_ADDFAV_DRAWABLE" />
</menu>
  • 然后创建您的 activity 代码

activity_example.java

package com.example;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

public class activity_example extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Layout xml init
        setContentView(R.layout.activity_example);

        //Actionbar
        //Back button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }



    //Inflate the menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_example, menu);
        return true;
    }

    //Handling Action Bar button click
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            //Back button
            case android.R.id.home:
                //If this activity started from other activity
                finish();

            /*If you wish to open new activity and close this one
            startNewActivity();
            */
                return true;
            case R.id.action_addfav:
                //addfav (heart icon) was clicked, Insert your after click code here.
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void startNewActivity(){
        Intent intent = new Intent(this,ACTIVITY_YOU_WANT_TO_START.class);
        startActivity(intent);
        finish();
    }

}
  • Activity布局xml

(您可以将根布局更改为您想要的任何布局)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- YOUR LAYOUT HERE -->
    </RelativeLayout>
  • 最后,确保您的应用在主题中有工具栏 转到 res/values/styles.xml

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <! -- Parent got to have actionbar in order for toolbat to appear -->

</style>

如果您希望工具栏只出现在一个特定的 activity 中,请查看:Apply a theme to an activity in Android?