【问题标题】:Toolbar Title and Icon in Center中心的工具栏标题和图标
【发布时间】:2018-03-29 21:16:09
【问题描述】:

我正在尝试使用自定义工具栏。我想要工具栏中心的图标和标题。我已经尝试了很多将它设置在中心,但我无法做到。我的 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>

但根据我的需要,它并不完美。我想要第一个徽标而不是工具栏中心的标题,但它看起来像这样

Look

如果有人可以帮助我,请告诉我。如果它非常简单,我真的很抱歉。我还在学习。谢谢

【问题讨论】:

标签: java android android-toolbar


【解决方案1】:

试试这个:

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

使用LinearLayout 可以让您轻松确保视图不重叠。我还在右侧添加了 16dp 的内边距,以补偿 android 自动添加到左侧的 16dp 边距。

【讨论】:

    【解决方案2】:

    尝试为图像视图提供布局边距并将文本视图中心水平对齐,这将为您提供所需的输出。

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

    【讨论】:

      【解决方案3】:

      试试这个!仅使用具有水平方向 imo 的 LinearLayout 会更容易一些。根据需要更改 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-17
        • 1970-01-01
        • 2014-12-19
        • 2020-12-03
        • 1970-01-01
        • 1970-01-01
        • 2015-09-30
        相关资源
        最近更新 更多