How can I detect a click on the ActionBar title?

陌路散爱 提交于 2019-12-17 10:56:25

问题


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?


回答1:


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().




回答2:


//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;
    }
}



回答3:


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;
            }
        });


来源:https://stackoverflow.com/questions/7981394/how-can-i-detect-a-click-on-the-actionbar-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!