Android-Activity Home/Up箭头在SDK 24中具有附加的填充/边距 [英] Android - Activity Home/Up arrow has additional padding/margin with SDK 24
问题描述
我刚刚将我的应用程序从使用SDK 23更新到了SDK 24.
I've just updated my app from using SDK 23 to SDK 24.
我的活动显示了向上/主页箭头(即getSupportActionBar().setDisplayHomeAsUpEnabled(true)
)时出现了问题,因为在向上箭头和活动标题之间现在有多余的(不需要的)空间.
A problem has arisen for my activities that have show the Up/Home arrow (i.e., getSupportActionBar().setDisplayHomeAsUpEnabled(true)
) in that there is now additional (unwanted) space between the Up arrow and the activity title.
对于没有上箭头的活动,活动标题与以前完全相同,这表明附加的填充/边距与上箭头而不是活动标题相关.
For activities that don't have an Up arrow, the activity title is in exactly the same place as before, which suggests the additional padding/margin is associated with the Up arrow rather than with the activity title.
我的问题是如何更改布局,以使其在SDK 24中看起来与在SDK 23中一样?
My question is how do I change the layout so that it looks the same with SDK 24 as it did with SDK 23?
使用SDK 23时,向上箭头和标题之间的距离很小:
使用SDK 24时,向上箭头和标题之间的间隙很大(不需要):
这是我的旧build.gradle(SDK 23):
Here is my old 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'
}
这是新的build.gradle(SDK 24):
Here is the new 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'
}
推荐答案
我认为此填充是与SDK 24中的Material规范相匹配的新标准. 首先,您必须通过以下代码隐藏默认的工具栏标题:
I think that this padding is a new standard to match Material spec in SDK 24. First you must hide default toolbar title by this code:
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowTitleEnabled(false);
然后创建一个名为toolbar.xml
的文件,并将以下代码添加到该文件中:
Then make a file called toolbar.xml
and add these codes in that file:
<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>
如您所见,您可以控制此文件中的所有内容.看一下toolbar.xml中的这一行:
As you see, you can control everything in this file. Look at this line in toolbar.xml :
app:contentInsetStartWithNavigation ="0dp"
app:contentInsetStartWithNavigation="0dp"
这就是您想要的.祝你好运.
This is what you want. Goodluck.
这篇关于Android-Activity Home/Up箭头在SDK 24中具有附加的填充/边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!