Android - Standard height of toolbar

后端 未结 6 1558
伪装坚强ぢ
伪装坚强ぢ 2021-01-30 15:59

I want to create a toolbar in my app, and I am wondering what is the standard height for the toolbar in android?

I want it to be big enough for a finger, but not huge.

6条回答
  •  后悔当初
    2021-01-30 16:29

    You can use the following method to get the AppBar height programatically

    private static final int DEFAULT_TOOLBAR_HEIGHT = 56;
    
    private static int toolBarHeight = -1;
    
    public static int getToolBarHeight(Context context) {
            if (toolBarHeight > 0) {
                return toolBarHeight;
            }
            final Resources resources = context.getResources();
            final int resourceId = resources.getIdentifier("action_bar_size", "dimen", "android");
            toolBarHeight = resourceId > 0 ?
                    resources.getDimensionPixelSize(resourceId) :
                    (int) convertDpToPixel(DEFAULT_TOOLBAR_HEIGHT);
            return toolBarHeight;
        }
    
    public static float convertDpToPixel(Context context, float dp) {
        float scale = context.getResources().getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }
    

提交回复
热议问题