在ActionBar的主页图标和标题之间填充

Ale*_*qui 107 android android-actionbar

有人知道如何在ActionBar的主页图标和标题之间设置填充吗?

Cli*_*fus 99

编辑:确保将此drawable设置为LOGO,而不是像某些评论者那样设置应用程序图标.

只需创建一个XML drawable并将其放在资源文件夹"drawable"中(没有任何密度或其他配置).

<?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>
Run Code Online (Sandbox Code Playgroud)

最后一步是将此新drawable设置为清单中的徽标(或活动中的Actionbar对象)

祝好运!

  • 确保将此drawable设置为LOGO,而不是将其设置为应用程序图标.祝好运! (3认同)

Min*_*sky 69

我修改了Cliffus的回答并在我的动作栏样式定义中分配了logo-drawable,例如在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>
Run Code Online (Sandbox Code Playgroud)

drawable在res/drawable/actionbar_space_between_icon_and_title.xml中看起来像Cliffus的一个(这里有默认的应用程序启动器图标):

<?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>
Run Code Online (Sandbox Code Playgroud)

在android_manifest.xml中,您仍然可以设置不同的应用程序图标("桌面"上的启动器图标.此处的任何不同徽标定义都可在没有操作栏的活动中看到.


Rah*_*ani 52

我也面临类似的问题,在我的情况下,我必须根据内容动态设置每个活动的标题.

所以这对我有用.

actionBar.setTitle("  " + yourActivityTitle);
Run Code Online (Sandbox Code Playgroud)

如果你想要的只是间距,这是我能想到的最简单的解决方案.

  • 为什么投票失败?不要认为这是一个错误的答案,只是另一种方式. (12认同)
  • 我不会选择这个解决方案,因为我真的需要在我的应用程序中更稳定,但我想问所有那些人说"它不干净".哦,我的,上帝,这个问题的CLEAN解决方案在哪里?所有那些带徽标的hacks,setPadding对我来说看起来也不干净!只是阅读评论,有多少人也有问题.在标题上设置android:paddingLeft - 如果它有效,那将是什么CLEAN.我们在黑暗中留下来发明黑客行为,因为出于某种原因,它只是按照应该的方式运作.所以没有什么是"不干净". (4认同)
  • @idratherbeintheair是的我同意这不是"最干净"的方式,所以现在它只说最简单.但是当你指出什么时,请发布一个替代解决方案.我遇到了这个问题,这就是我在我的应用中使用的内容,所以我分享了. (3认同)
  • 真?在具有不同DPI的不同屏幕上,此解决方案可能无法正常工作. (3认同)

小智 40

这就是我能够在主页图标和标题之间设置填充的方法.

ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(left, top, right, bottom);
Run Code Online (Sandbox Code Playgroud)

我找不到通过ActionBar xml样式自定义的方法.也就是说,以下XML不起作用:

<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>
Run Code Online (Sandbox Code Playgroud)

但是,如果您希望通过xml实现此目的,这两个链接可能会帮助您找到解决方案:

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

(这是用于在操作栏中显示主页图标的实际布局) https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/action_bar_home.xml


the*_*ott 32

这是Material Design中的常见问题,因为您可能希望将工具栏标题与下面片段中的内容对齐.为此,您可以使用toolbar.xml布局中的属性contentInsetStart ="72dp"覆盖默认的"12dp"填充,如下所示

toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/app_theme_color"
    app:contentInsetStart="72dp"/>
Run Code Online (Sandbox Code Playgroud)

然后在你的活动中,打电话

Toolbar toolbar = (Toolbar) activity.findViewById(R.id.toolbar);
toolbar.setTitle("Title Goes Here");
Run Code Online (Sandbox Code Playgroud)

你最终得到了这个:

工具栏

  • 这是执行此操作的正确方法.还有`app:titleMarginStart ="dimension"`直接定位标题. (11认同)

Aji*_*jit 29

对我来说,只有以下组合起作用,从API 18到24测试

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
Run Code Online (Sandbox Code Playgroud)

在"app"中的位置是: xmlns:app="http://schemas.android.com/apk/res-auto"

例如.

 <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>
Run Code Online (Sandbox Code Playgroud)

  • 这个答案需要更多的投票才能提升 (2认同)

try*_*ryp 29

如果您使用AppCompat中的工具栏(android.support.v7.widget.Toolbar,> = Revision 24,2016年6月),则可以使用以下值更改图标和标题之间的填充:

  app:contentInsetStartWithNavigation="0dp"
Run Code Online (Sandbox Code Playgroud)

为了改进我的答案,您可以直接在活动中的工具栏上使用它,也可以为工具栏创建新的布局文件.在这种情况下,您只需要在每个所需视图上使用include属性导入工具栏的@id.

<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>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在activity.xml上导入布局

<include
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)


Dru*_*uid 5

使用titleMarginStart作品给我.Xamarin示例:

<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"/>
Run Code Online (Sandbox Code Playgroud)

像这样设置徽标:

mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
SetSupportActionBar(mToolbar);
SupportActionBar.SetLogo(Resource.Drawable.titleicon32x32);
SupportActionBar.SetDisplayShowHomeEnabled(true);
SupportActionBar.SetDisplayUseLogoEnabled(true);
SupportActionBar.Title = "App title";
Run Code Online (Sandbox Code Playgroud)