java - AppCompat SupportActionBar 有关图像放置的问题

标签 java android gradle android-appcompat android-styles

我有一个问题。我有一个支持操作栏,我在其中设置了图像。问题是图像稍微偏右。我认为这是一个兼容性问题。

这是图像的屏幕截图:screenshot

如您所见,actionBar 中的图像稍微偏右(如果看不到,请相信我)。

当我使用actionBar时,图像最初正确居中,但是当我将appcompat添加到项目中时,getActionBar()开始返回null,并且应用程序崩溃了。我搜索了互联网并找到了 getSupportActionBar() 方法。这次应用程序没有崩溃,但图像没有像以前那样正确居中。我怀疑这是主题/兼容性/覆盖的问题,或类似的事情。

有人可以帮助我吗?

这是我调用操作栏的方法:

 public void initializeActionBar() {
    getSupportActionBar();
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    getSupportActionBar().setCustomView(com.entu.bocterapp.R.layout.action_bar_center_image);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff50aaf1));
    getSupportActionBar().setDisplayShowTitleEnabled(false);
}

以下是“R.layout.action_bar_center_image”的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:orientation="horizontal">
<ImageView
    android:id="@+id/bocterAppLogo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/bocterapp"
    android:contentDescription=""/>
</LinearLayout>

这是 gradle.build:

buildscript {
repositories {
    mavenCentral()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.0.0-rc1'
}
}
    apply plugin: 'android'

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    compile files('libs/Parse-1.7.0/Parse-1.7.0.jar')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.google.android.gms:play-services:6.5.87'
}
android {
    signingConfigs {
        config {
            storeFile file('C:/bocterapp.keystore')
        }
    }
    compileSdkVersion 21
    buildToolsVersion '21.1.1'
    sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

    // Move the tests to tests/java, tests/res, etc...
    instrumentTest.setRoot('tests')

    // Move the build types to build-types/<type>
    // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
    // This moves them out of them default location under src/<type>/... which would
    // conflict with src/ being used by the main source set.
    // Adding new build types or product flavors should be accompanied
    // by a similar customization.
    debug.setRoot('build-types/debug')
    release.setRoot('build-types/release')
}
dexOptions {
    preDexLibraries = false
}
}

以下是样式:

样式:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<color name="azure">#50aaf1</color>
<color name="black">#302D2D</color>
<color name="white">#ffffff</color>

v11/:

<resources>

<!--
    Base application theme for API 11+. This theme completely replaces
    AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!-- API 11 theme customizations can go here. -->
</style>

v14/:

<resources>

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

有人可以帮助我吗? 干杯!

最佳答案

在 AppCompat 21 中,操作栏由工具栏小部件表示。

android.support.v7.widget.Toolbar 添加到您的 Activity 布局。

然后设置app:contentInsetStart="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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp" >


     //Put here your layout.

</android.support.v7.widget.Toolbar>

在您的 Activity 中:

Toolbar actionBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(actionBar);

顺便说一句,我建议您遵循 Material 指南。 Android 不像 IOS 那样使用操作栏居中的图像。

关于java - AppCompat SupportActionBar 有关图像放置的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28380789/

相关文章:

java - 今天第一次使用 eclipse...而且 eclipse 我真的不喜欢它。还有哪些其他更清洁的替代方案?

java - 反转 HashMap 中的键值对

java - 代码在 JavaSE 中有效,但在 Wildfly 中因 NullPointerException 而崩溃

Android(谷歌登录): how to verify the integrity of the ID token on the server?

android - 如何检查一个方法是否没有被 mockk 调用?

gradle - 从 Spring Contract 的合约和基类测试的默认 "test"目录更改

android - 创建或解析Java和Kotlin分类成Gradle任务

java - Slick2d 使用动画/图像抗锯齿

android - 如何确定 android 的正确字体大小?

android - 如何同时使用Android NDK和Fabric?