How can I detect a click on the ActionBar title?

2019-01-11 12:42发布

For specific customer requirement, I need to allow the user of my app ( won't be published in Market) to click on the ActionBar title to execute some actions.

I have been looking in the Android source, but I am not able to find an ID for the actionBar TextView title.

Is there a proper way to handle such clicks?

3条回答
狗以群分
2楼-- · 2019-01-11 13:13

You can set up a custom toolbar from Support Library by declaring <android.support.v7.widget.Toolbar> in your layout (see Chris Banes' answer for full toolbar layout example).

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- We use a Toolbar so that our drawer can be displayed
             in front of the action bar -->
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_awesome_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/main_toolbar" 
            android:minHeight="?attr/actionBarSize" />

       <FrameLayout 
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />


</LinearLayout>

After you can add on click listener in your activity just like to most other Views.

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MyActivity.this, "Test", Toast.LENGTH_LONG).show();
            }
        });

If you want to capture touch events on title:

toolbar.setOnTouchListener(new View.OnTouchListener() {
            Rect hitrect = new Rect();
            public boolean onTouch(View v, MotionEvent event) {
                if (MotionEvent.ACTION_DOWN == event.getAction()) {
                    boolean hit = false;
                    for (int i = toolbar.getChildCount() - 1; i != -1; i--) {
                        View view = toolbar.getChildAt(i);
                        if (view instanceof TextView) {
                            view.getHitRect(hitrect);
                            if (hitrect.contains((int)event.getX(), (int)event.getY())) {
                                hit = true;
                                break;
                            }
                        }
                    }
                    if (hit) {
                        //Hit action
                    }
                }
                return false;
            }
        });
查看更多
ら.Afraid
3楼-- · 2019-01-11 13:18

//onCreate

ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
//        View actionBarView = getLayoutInflater().inflate(R.layout.action_bar_custom_view, null);
        actionBar.setCustomView(actionBarView);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

//your logic for click listner
 setListenerForActionBarCustomView(actionBarView);

 private void setListenerForActionBarCustomView(View actionBarView) {
        ActionBarCustomViewOnClickListener actionBarCustomViewOnClickListener = new ActionBarCustomViewOnClickListener();
        actionBarView.findViewById(R.id.text_view_title).setOnClickListener(actionBarCustomViewOnClickListener);
}
 private class ActionBarCustomViewOnClickListener implements OnClickListener {
        public void onClick(View v) {       
        switch(v.getId()) {
            case R.id.text_view_title:

                //finish();
                break;
    }
}
查看更多
家丑人穷心不美
4楼-- · 2019-01-11 13:23

The title is non-clickable AFAIK. The icon/logo is clickable -- you'll get that via android.R.id.home in onOptionsItemSelected(). Conceivably, the title also routes this way, though they don't mention it and I wouldn't rely upon it.

It sounds like you want a Spinner for the user to choose the actions to execute. If so, use setListNavigationCallbacks(). If you want to remove the title as now being superfluous, use setDisplayOptions(0, DISPLAY_SHOW_TITLE).

If you want something other than a Spinner on the left side of the action bar, use setDisplayOptions(DISPLAY_SHOW_CUSTOM, DISPLAY_SHOW_CUSTOM) and setCustomView(). Note that this approach is not recommended ("Avoid using custom navigation modes in the action bar"), as it may not work well with phones, particularly in portrait mode.

Another possibility would be to remove the title and use a logo instead of the icon, and in the logo have your "title" as part of the image. The whole logo should be clickable, picked up via onOptionsItemSelected().

查看更多
登录 后发表回答