Android - Standard height of toolbar

后端 未结 6 1553
伪装坚强ぢ
伪装坚强ぢ 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:07

    Its best to use ?attr/actionBarSize as @Jaison Brooks commented.

    In the material guidelines, suggested height is 56dp:

    Toolbar: 56dp

    0 讨论(0)
  • 2021-01-30 16:07

    The recommended minimum size for touchable elements is 48 dp, see this page for more detailed metrics.

    0 讨论(0)
  • 2021-01-30 16:20

    For phones it is 56dp and for large devices like tablets which you have more spaces it could be 64dp

    0 讨论(0)
  • 2021-01-30 16:21

    you can use the toolbar widget that already exist in android and put the height wrap_content , so it will better to get the default size that come with it.

    here

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/dark_cerulean">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingEnd="16dp"
            android:paddingStart="16dp">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="end"
            android:gravity="end"
            android:layout_marginEnd="16dp"
            android:textColor="@color/white"
            android:id="@+id/toolbar_title" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/image1"
                android:id="@+id/image"/>
    
        </LinearLayout>
    
    
    </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
  • 2021-01-30 16:26

    In addition to @vedant1811 answer, you can programmatically obtain actionBarSize from attrs:

    TypedValue tv = new TypedValue();
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
    {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题