Padding between ActionBar's home icon and title

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

    When you are using a custom Toolbar, you can use

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(R.string.activity_title);
    setSupportActionBar(toolbar);
    getSupportActionBar().setLogo(R.drawable.logo);
    

    and in your toolbar layout simply set app:titleMarginStart="16dp"

    Note that you have to set the icon as a logo, don't use getSupportActionBar().setIcon(R.drawable.logo) instead use: getSupportActionBar().setLogo(R.drawable.logo)

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

    I also faced a similar issue, in my case I had to set titles dynamically on each activity depending on the content.

    So this worked for me.

    actionBar.setTitle("  " + yourActivityTitle);
    

    If all you want is the spacing, this is the easiest solution I could think of.

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

    This is how I was able to set the padding between the home icon and the title.

    ImageView view = (ImageView)findViewById(android.R.id.home);
    view.setPadding(left, top, right, bottom);
    

    I couldn't find a way to customize this via the ActionBar xml styles though. That is, the following XML doesn't work:

    <style name="ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">        
        <item name="android:titleTextStyle">@style/ActionBarTitle</item>
        <item name="android:icon">@drawable/ic_action_home</item>        
    </style>
    
    <style name="ActionBarTitle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textSize">18sp</item>
        <item name="android:paddingLeft">12dp</item>   <!-- Can't get this padding to work :( -->
    </style>
    

    However, if you are looking to achieve this through xml, these two links might help you find a solution:

    https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml

    (This is the actual layout used to display the home icon in an action bar) https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/action_bar_home.xml

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

    I used AppBarLayout and custom ImageButton do to so.

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:background="@android:color/transparent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
       <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">
    
           <ImageView
               android:layout_width="32dp"
               android:layout_height="32dp"
               android:src="@drawable/selector_back_button"
               android:layout_centerVertical="true"
               android:layout_marginLeft="8dp"
               android:id="@+id/back_button"/>
    
           <android.support.v7.widget.Toolbar
               android:id="@+id/toolbar"
               android:layout_width="match_parent"
               android:layout_height="?attr/actionBarSize"
               app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        </RelativeLayout>    
    </android.support.design.widget.AppBarLayout>
    

    My Java code:

    findViewById(R.id.appbar).bringToFront();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    final ActionBar ab = getSupportActionBar();
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    
    0 讨论(0)
  • 2020-11-28 20:41

    Using titleMarginStart works for me. Xamarin example:

    <android.support.v7.widget.Toolbar
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/toolbar"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:minHeight="?attr/actionBarSize"
      android:background="?attr/colorPrimary"
      app:titleMarginStart="24dp"/>
    

    Set the logo like so:

    mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
    SetSupportActionBar(mToolbar);
    SupportActionBar.SetLogo(Resource.Drawable.titleicon32x32);
    SupportActionBar.SetDisplayShowHomeEnabled(true);
    SupportActionBar.SetDisplayUseLogoEnabled(true);
    SupportActionBar.Title = "App title";
    
    0 讨论(0)
  • 2020-11-28 20:42

    If you are using toolbar from AppCompat (android.support.v7.widget.Toolbar, >= revision 24, June 2016), the padding between the icon and the title can be changed with the following value :

      app:contentInsetStartWithNavigation="0dp"
    

    To improve my answer, you can use it on your toolbar directly inside your activity or you can create a new layout file for your toolbar. In this case, you just have to import your toolbar's @id using the include property on each needed views.

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="4dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp">
    </android.support.v7.widget.Toolbar>
    

    You can then import your layout on your activity.xml

    <include
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    0 讨论(0)
提交回复
热议问题