代码之家  ›  专栏  ›  技术社区  ›  Janitha Prabath Madushanka

将onclick侦听器添加到工具栏上的图标[复制]

  •  0
  • Janitha Prabath Madushanka  · 技术社区  · 7 年前

    [我的工具栏][1]

    有人有什么解决办法吗?

    3 回复  |  直到 7 年前
        1
  •  4
  •   Dor Rud    7 年前

    我看到了你的工具栏。 以下是我的解决方案:

    • 首先创建一个菜单xml资源文件。 (这将包含您的“心脏图标”,res/menu/menu\u example.xml)

    请随意命名,例如,目的我将其命名为menu\u example。

    menu\u示例。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\u示例。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();
        }
    
    }
    
    • 活动布局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>
    
    • 最后,确保你的应用在主题中有工具栏 转到“分辨率/值/样式”。xml

    样式。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>
    

    如果希望工具栏仅位于一个特定活动中,请查看: Apply a theme to an activity in Android?

        2
  •  0
  •   Abdul Waheed    7 年前

    你需要检查一下 R、 id.img\u bar\u返回 . 它是您正在查找的ImageView id还是其他id。这可能是因为找到了错误的视图id。

        3
  •  0
  •   Ways Test    7 年前
     <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>