AppCompat SupportActionBar有关图像放置的问题

时间:2015-02-07 10:01:07

标签: 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>

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

1 个答案:

答案 0 :(得分:1)

使用AppCompat 21,操作栏由工具栏小部件表示。

在您的活动布局中添加android.support.v7.widget.Toolbar

然后设置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>

在您的活动中:

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

BTW我建议您遵循材料指南。 Android不会将操作栏中的图像用作IOS。