Padding between ActionBar's home icon and title

前端 未结 21 1545
名媛妹妹
名媛妹妹 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:19

    I used this method setContentInsetsAbsolute(int contentInsetLeft, int contentInsetRight) and it works!

    int padding = getResources().getDimensionPixelSize(R.dimen.toolbar_content_insets);
    mToolbar.setContentInsetsAbsolute(padding, 0);
    
    0 讨论(0)
  • 2020-11-28 20:23

    this work for me to add padding to the title and for ActionBar icon i have set that programmatically.

     getActionBar().setTitle(Html.fromHtml("<font color='#fffff'>&nbsp;&nbsp;&nbsp;Boat App </font>"));
    
    0 讨论(0)
  • 2020-11-28 20:26

    I adapted Cliffus answer and assigned the logo-drawable in my actionbar style definition, for instance like this in res/style.xml:

    <item name="android:actionBarStyle">@style/MyActionBar</item>
    
    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
            <item name="android:background">#3f51b5</item>
            <item name="android:titleTextStyle">@style/ActionBar.TitleText</item>
            <item name="android:textColor">#fff</item>
            <item name="android:textSize">18sp</item>
            <item name="android:logo">@drawable/actionbar_space_between_icon_and_title</item>
    </style>
    

    The drawable looks like Cliffus' one (here with the default app launcher icon) in res/drawable/actionbar_space_between_icon_and_title.xml:

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

    In the android_manifest.xml you can still set a different app icon (launcher icon on 'desktop'. Any different logo definition here are visible in activities without an action bar.

    0 讨论(0)
  • 2020-11-28 20:26
    <string name="app_name">"    "Brick Industry</string>
    

    Just add " " for your app name It will add space between icon and title

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

    I used \t before the title and worked for me.

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

    I solved this problem by using custom Navigation layout

    Using it you can customize anything in title on action bar:

    build.gradle

    dependencies {
        compile 'com.android.support:appcompat-v7:21.0.+'
        ...
    }
    

    AndroidManifest.xml

    <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name">
            <activity
                android:name=".MainActivity"
                android:theme="@style/ThemeName">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    </application>
    

    styles.xml

    <style name="ThemeName" parent="Theme.AppCompat.Light">
       <item name="actionBarStyle">@style/ActionBar</item>
       <item name="android:actionBarStyle" tools:ignore="NewApi">@style/ActionBar</item>
    

    <style name="ActionBar" parent="Widget.AppCompat.ActionBar">
       <item name="displayOptions">showCustom</item>
       <item name="android:displayOptions" tools:ignore="NewApi">showCustom</item>
    
        <item name="customNavigationLayout">@layout/action_bar</item>
       <item name="android:customNavigationLayout" tools:ignore="NewApi">@layout/action_bar</item>
    
        <item name="background">@color/android:white</item>
       <item name="android:background">@color/android:white</item>
    

    action_bar.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/action_bar_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center_vertical"
              android:drawableLeft="@drawable/ic_launcher"
              android:drawablePadding="10dp"
              android:textSize="20sp"
              android:textColor="@android:color/black"
              android:text="@string/app_name"/>
    
    0 讨论(0)
提交回复
热议问题