2016-02-03 36 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