2015-02-07 26 views
0

我遇到問題。我有一個支持行動欄,我在其中設置了一個圖像。問題是圖像稍微偏右。我認爲這是一個兼容性問題。AppCompat SupportActionBar有關圖像放置的問題

這裏是圖像的截圖:screenshot

正如你所看到的,在動作條的圖像稍微向右(如果它是不可見的,相信我,它是)。

圖像最初正確居中,當我使用actionBar時,但是當我將appcompat添加到項目時,getActionBar()開始返回null,並且應用程序崩潰。我搜索了互聯網並得到了getSupportActionBar()方法。這次應用程序沒有崩潰,但圖像沒有像以前那樣正確居中。我懷疑這是一個主題/兼容性/重寫或類似的事情。

任何人都可以幫助我嗎?

下面是其中i的調用操作欄中的方法:

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> 

有人可以善待我嗎? 乾杯!

+0

您可以發佈您在操作欄中設置的圖片照片嗎?因爲我想檢查實際的照片,代碼似乎是正確的,所以可能有圖像本身的問題,所以請編輯您的問題,並添加實際圖像 – Apurva 2015-02-07 10:07:16

回答

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); 

順便說一句,我建議你將以下材料管理準則。 Android不會在操作欄中使用以IOS爲中心的圖像。

+0

謝謝,它的工作!關於材料準則:這是我如何接受應用程序的設計,不能做太多的事情。 – 2015-02-10 16:02:33