Android - 活动主页/向上箭头具有SDK 24的额外填充/边距

ban*_*ing 19 android android-layout android-sdk-tools android-actionbar android-actionbar-compat

我刚刚使用SDK 23更新了我的应用程序到SDK 24.

我的活动出现了一个问题,即显示向上/向主箭头(即getSupportActionBar().setDisplayHomeAsUpEnabled(true)),现在在向上箭头和活动标题之间存在额外的(不需要的)空间.

对于没有向上箭头的活动,活动标题与之前完全相同,这表示额外的填充/边距与向上箭头相关联,而不是与活动标题相关联.

我的问题是如何更改布局,使其与SDK 23看起来与使用SDK 23时相同?

使用SDK 23在向上箭头和标题之间的小差距: 使用SDK 23在向上箭头和标题之间的小差距

使用SDK 24在向上箭头和标题之间存在较大(不需要的)差距: 使用SDK 24在向上箭头和标题之间存在较大(不需要的)差距

这是我的旧build.gradle(SDK 23):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 42
        versionName "0.42"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services-vision:9.0.2'
    compile 'ch.acra:acra:4.7.0'
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:recyclerview-v7:23.4.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:support-v13:23.4.0'
    compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
    compile 'com.google.zxing:core:3.2.1'
}
Run Code Online (Sandbox Code Playgroud)

这是新的build.gradle(SDK 24):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 42
        versionName "0.42"
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.google.android.gms:play-services-vision:9.0.2'
    compile 'ch.acra:acra:4.7.0'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:support-v13:24.0.0'
    compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
    compile 'com.google.zxing:core:3.2.1'
}
Run Code Online (Sandbox Code Playgroud)

小智 26

这在Android问题跟踪器中得到了解答.链接是:

https://code.google.com/p/android/issues/detail?id=213826

添加app:contentInsetStartWithNavigation="0dp"到工具栏视图.


Hos*_*ifi 22

我认为这个填充是与SDK 24中的Material spec匹配的新标准.首先,您必须通过以下代码隐藏默认工具栏标题:

getSupportActionBar()setDisplayShowTitleEnabled(假).

然后创建一个名为的文件toolbar.xml并在该文件中添加这些代码:

<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="56dp"
    android:background="@color/primary_color"
    app:theme="@style/ThemeOverlay.AppCompat"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:contentInsetStartWithNavigation="0dp">

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp" <!-- Add margin -->
        android:layout_marginStart="16dp"
        android:gravity="left|center"
        android:text="Toolbar Title" <!-- Your title text -->
        android:textColor="@color/white" <!-- Matches default title styles -->
        android:textSize="20sp"
        android:fontFamily="sans-serif-medium"/>

</android.support.v7.widget.Toolbar>
Run Code Online (Sandbox Code Playgroud)

如您所见,您可以控制此文件中的所有内容.在toolbar.xml中查看这一行:

应用程式:contentInsetStartWithNavigation = "0dp"

这就是你想要的.祝好运.