how to center icons in toolbar in android

后端 未结 7 2028
清歌不尽
清歌不尽 2021-02-07 21:29

I asked a similar question here... I got some tutorials in the answers. But this question is diffrenet. because none of that method do not works in my project.

I want

7条回答
  •  一向
    一向 (楼主)
    2021-02-07 21:49

    you can do it programmatically as follows:

    mToolbar.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ImageView imageView = (ImageView)mToolbar.findViewById(R.id.logo);
                if( mToolbar == null ){
                    return;
                }
                int toolbarWidth = mToolbar.getWidth();
                int imageWidth = imageView.getWidth();
                imageView.setX((toolbarWidth-imageWidth)/2);
            }
        });
    

    customise your layout_toolbar.xml:

        
                
    
        
    

提交回复
热议问题