Padding between ActionBar's home icon and title

前端 未结 21 1547
名媛妹妹
名媛妹妹 2020-11-28 20:03

Does anybody know how to set padding between the ActionBar\'s home icon and the title?

相关标签:
21条回答
  • 2020-11-28 20:28

    You can change drawableSize of your DrawerArrow like this:

    <style name="MyTheme" parent="android:Theme.WithActionBar">
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>
    
    <style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
        <item name="barLength">24dp</item>
        <item name="arrowShaftLength">24dp</item>
        <item name="arrowHeadLength">10dp</item>
        <item name="drawableSize">42dp</item> //this is your answer
    </style>
    

    It isn't correct answer, because you can't choose padding side and DrawerArrow icon scaling when change drawableSize (drawableSize = width = height). But you can margin from left. To margin from right do

    findViewById(android.R.id.home).setPadding(10, 0, 5, 0);
    
    0 讨论(0)
  • 2020-11-28 20:29

    For my case, it was with Toolbar i resolved it like this:

    ic_toolbar_drawble.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/ic_toolbar"
        android:right="-16dp"
        android:left="-16dp"/>
    </layer-list>
    

    In my Fragment, i check the api :

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
          toolbar.setLogo(R.drawable.ic_toolbar);
    else
          toolbar.setLogo(R.drawable.ic_toolbar_draweble);
    

    Good luck!

    0 讨论(0)
  • 2020-11-28 20:29

    You can achieve same by method as well:

    Drawable d = new InsetDrawable(getDrawable(R.drawable.nav_icon),0,0,10,0);
    mToolbar.setLogo(d);
    
    0 讨论(0)
  • 2020-11-28 20:32

    EDIT: make sure you set this drawable as LOGO, not as your app icon like some of the commenters did.

    Just make an XML drawable and put it in the resource folder "drawable" (without any density or other configuration).

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item
            android:drawable="@drawable/my_logo"
            android:right="10dp"/>
    
    
    </layer-list>
    

    The last step is to set this new drawable as logo in your manifest (or on the Actionbar object in your activity)

    Good luck!

    0 讨论(0)
  • 2020-11-28 20:32

    In your XML, set the app:titleMargin in your Toolbar view as following:

    <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:titleMarginStart="16dp"/>
    

    Or in your code:

    toolbar.setTitleMargin(16,16,16,16); // start, top, end, bottom
    
    0 讨论(0)
  • 2020-11-28 20:33

    For me only the following combination worked, tested from API 18 to 24

    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    

    where in "app" is : xmlns:app="http://schemas.android.com/apk/res-auto"

    for example.

     <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@color/SE_Life_Green"
                    app:contentInsetLeft="0dp"
                    app:contentInsetStart="0dp"
                    app:contentInsetStartWithNavigation="0dp"
                    >
                    .......
                    .......
                    .......
                </android.support.v7.widget.Toolbar>
    
    0 讨论(0)
提交回复
热议问题