我刚刚将我的应用程序从使用 SDK 23 更新为 SDK 24。
我的活动出现了一个问题,显示了向上/主页箭头(即
getSupportActionBar().setDisplayHomeAsUpEnabled(true)
),因为向上箭头和活动标题之间现在有额外的(不需要的)空间。
对于没有向上箭头的活动,活动标题与之前的位置完全相同,这表明额外的填充/边距与向上箭头相关联,而不是与活动标题相关联。
我的问题是如何更改布局,使其在 SDK 24 中看起来与在 SDK 23 中一样?
使用 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'
}
这是新的 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'
}
这个问题已在 Android 问题跟踪器中得到解答。链接是:
https://code.google.com/p/android/issues/detail?id=213826
将
app:contentInsetStartWithNavigation="0dp"
添加到工具栏视图。
我认为这个填充是一个新的标准,以匹配 SDK 24 中的材质规范。 首先,您必须通过以下代码隐藏默认工具栏标题:
getSupportActionBar().setDisplayShowTitleEnabled(false);
然后创建一个名为
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>
如您所见,您可以控制此文件中的所有内容。查看toolbar.xml 中的这一行:
应用程序:contentInsetStartWithNavigation =“0dp”
这就是你想要的。祝你好运。
尝试使用此代码删除不需要的空间 根据您的用途使用它:-
<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_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/red"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp"
android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>
我设法仅使用 XML 来完成此操作,无需添加自定义工具栏(但使用默认工具栏)。
styles.xml
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!--
if you want to further customize the home/up icon,
uncomment the following and specify a drawable
-->
<!--<item name="homeAsUpIndicator">@null</item>-->
<item name="toolbarStyle">@style/ActionBar.NoPad</item>
</style>
<!-- Remove padding with home/up arrow -->
<style name="ActionBar.NoPad" parent="@style/Widget.AppCompat.Toolbar">
<item name="contentInsetStartWithNavigation">0dp</item>"
</style>