简体   繁体   English

中心的工具栏标题和图标

[英]Toolbar Title and Icon in Center

I am trying to use custom toolbar. 我正在尝试使用自定义工具栏。 I want icon and title in center of my toolbar. 我希望图标和标题位于工具栏的中心。 I have tried lot for set it in center but I am unable to do it. 我已经尝试过很多将其设置在中心的操作,但是我做不到。 My XML is like below 我的XML如下所示

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="?actionBarSize"
        android:layout_width="match_parent"
        android:background="@color/colorPrimary">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <ImageView
                android:padding="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher"/>

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:gravity="center"
                android:text="MyApplication"
                android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
                android:textColor="@color/colorWhite" />

        </RelativeLayout>


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

But its not display perfect as per my need. 但是根据我的需求,它的显示效果并不理想。 I want first logo and than title in center of toolbar but its look like this 我想要第一个徽标,而不是标题在工具栏中心,但它看起来像这样

Look

Let me know if someone can help me for do it. 让我知道是否有人可以帮助我。 I am really sorry if its very simple. 如果真的很简单,我真的很抱歉。 I am learning yet. 我正在学习。 Thanks 谢谢

Try this instead: 尝试以下方法:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?actionBarSize"
    android:layout_width="match_parent"
    android:paddingRight="16dp"
    android:background="@color/colorPrimary">

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

        <ImageView
            android:padding="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_launcher"/>

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="MyApplication"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:textColor="@color/colorWhite" />

    </LinearLayout>

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

Using LinearLayout will allow you to easily make sure the views don't overlap. 使用LinearLayout可以轻松确保视图不重叠。 I also added 16dp of padding to the right side to compensate for the 16dp margin android automatically adds to the left side. 我还向右侧添加了16dp的填充,以补偿android自动添加至左侧的16dp保证金。

Try this! 尝试这个! A little bit easier to just use a LinearLayout with a horizontal orientation imo. 仅使用具有水平方向imo的LinearLayout会容易一些。 Change the imageview source and title text as needed: 根据需要更改imageview源和标题文本:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?actionBarSize"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary">

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_horizontal">
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_launcher_background"/>
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Title"/>
    </LinearLayout>

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

Try giving layout margin for the image view and Align the textview center horizontal which will give you your desired output. 尝试为图像视图提供布局边距,然后将textview的中心水平对齐,这将为您提供所需的输出。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="?actionBarSize"
    android:layout_width="match_parent"
    android:background="@color/colorPrimary">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="300dp"
            android:padding="5dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="MyApplication"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:textColor="@color/colorWhite" />

    </RelativeLayout>


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

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

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