2016-02-03 39 views
0

這是我的menu-main.xml。我怎樣才能把我的標誌放在Android Action Bar的中心?

代碼下方的圖片顯示了我想將徽標放在我的應用程序中的位置。

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="com.example.akam.billettogram.MainActivity">**strong text** 
    <item 
     android:id="@+id/logo" 
     app:showAsAction="always" 
     android:title="" 
     android:layout_gravity="center" 
     android:icon="@drawable/logo"/> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="never"/> 
</menu> 

Position that I want to put my logo in.

+1

不要。 Android應用程序在操作欄的中央沒有徽標,應該使用操作欄爲用戶所在的屏幕提供上下文。例如:照片庫應該在操作欄中有文字「照片」或「照片庫」,而不是徽標。 – CodyEngel

+0

確定感謝您的幫助 – 1987akam

+1

您將不得不定義一個帶有文本框(帶有中心重力)的新工具欄項目,並將您的活動的主題設置爲NoActionBar。我會在一秒內給出答案的例子。 –

回答

1

所以,爲你做它是建立在Android Studio中新活動,讓最簡單的方法確定它是而不是空白

此模板將構建所需的所有代碼:

  • 它會創建Activity.java
  • AppBarLayout,ToolbarCoordinatorLayout創建佈局。
  • 它會在您的res /值中創建所需的款式
  • 它將在Manifest(NoActionBar)中將適當的風格設置爲活動。

所以...你將只需要添加您的圖標,也許TextView內自動創建Toolbar(在layout.xml)所看到的下面的代碼片段

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

    <LinearLayout 
     android:gravity="center" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <ImageView 
      android:src="@android:drawable/btn_star_big_off" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:text="Hello world" 
      android:textColor="@android:color/white" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

    </LinearLayout> 

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

下面是與所述結果的圖像:

enter image description here