简体   繁体   English

如何将徽标放在Android Action Bar的中心?

[英]How can i put my logo on Center of Android Action Bar?

This is my menu-main.xml. 这是我的menu-main.xml。

The picture below the code shows where I want to put the logo on my app. 代码下方的图片显示了我想在应用程序上放置徽标的位置。

<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. 我想放入徽标的位置。

So, the easiest way for you to do it is creating a new Empty Activity in Android Studio, make sure it is Empty and not Blank ! 因此,最简单的方法是在Android Studio中创建一个新的Empty Activity,确保它为Empty而不是Blank

This template will build all the code needed: 该模板将构建所需的所有代码:

  • It will create your Activity.java . 它将创建您的Activity.java
  • Create the layout with an AppBarLayout , Toolbar and CoordinatorLayout . 使用AppBarLayoutToolbarCoordinatorLayout创建布局。
  • It will create the needed styles in your res/values . 它将在您的res / values中创建所需的样式
  • It will set the Activity with the proper style in the Manifest (NoActionBar). 它将在清单(NoActionBar)中以适当的样式设置Activity。

So... you will just have to add your icon and maybe a TextView inside the automatically created Toolbar (in the layout.xml ) as seen in the following snippet : 所以...您只需要在自动创建的Toolbar (在layout.xml中 )内添加图标 ,也许还可以添加TextView ,如以下代码片段所示

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

Below is an image with the result: 下面是带有结果的图像:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM