0

今天我已经陷入了一个奇怪的问题Toolbar自定义标题。
我在中心使用TextView,在Toolbar的右侧使用ImageView。所以我在xml中使用了下面的代码行,请检查一次。工具栏标题的项目对齐问题?

<android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:theme="@style/AppTheme.NoActionBar.AppBarOverlay"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

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

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

        <TextView 
         android:id="@+id/headerTxt" 
         style="@style/Base.TextAppearance.AppCompat.Medium" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:ellipsize="marquee" 
         android:gravity="center" 
         android:layout_gravity="center" 
         android:maxLines="1" 
         android:text="Tile" 
         android:textColor="@android:color/black" /> 


        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/chat_new_icon" 
         android:layout_gravity="right" 
         /> 

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

并从上面的代码获取这些输出。 enter image description here

我已经使用了RelativeLayout它从这个问题出来,但它也不是worked.Please检查下面的代码为它

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

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

        <TextView 
         android:id="@+id/headerTxt" 
         style="@style/Base.TextAppearance.AppCompat.Medium" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:ellipsize="marquee" 
         android:gravity="center" 
         android:layout_gravity="center" 
         android:layout_centerInParent="true" 
         android:maxLines="1" 
         android:text="Tile" 
         android:textColor="@android:color/black" /> 


        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/chat_new_icon" 
         android:layout_alignParentRight="true" 
         android:layout_gravity="right" 
         /> 

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

并获得另一个意想不到的结果,请一次检查。 enter image description here

我已经提到这个解决方案,但它不适合我,请检查链接一次。
1. First Link
2. Second Link
3. Third Link
4. Forth Link

我需要什么是TextView应在ToolbarImageView中心应在Toolbar右侧。请帮我把短路从这个问题。

+1

尝试添加应用程序:contentInsetEnd = “0dp” 应用:contentInsetEndWithActions = “0dp” 应用:contentInsetLeft = “0dp” 应用:contentInsetRight = “0dp” 应用:contentInsetStart = “0dp” 应用程序: contentInsetStartWithNavigation =“0dp”在工具栏 –

+0

@DivyeshPatel已尝试urs建议不工作:( –

+0

也尝试设置内部布局高度以匹配父母 –

回答

0

@MikeM。建议已为我工作。有关问题的更多细节,请参考以下链接click here

我所做的一切得到从问题驱除掉是我已删除了ToolbarLinearLayout和设定的android:layout_gravity帮助视图的严重性,请请参考下面的解决方案

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

       <TextView 
        android:id="@+id/headerTxt" 
        style="@style/Base.TextAppearance.AppCompat.Medium" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:ellipsize="marquee" 
        android:maxLines="1" 
        android:text="Tile" 
        android:textColor="@android:color/black" /> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="right" 
        android:layout_marginRight="10dp" 
        android:src="@drawable/chat_new_icon" /> 


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

并从上面的代码得到预期的结果,请检查一次。 Output

0

只需更新代码...编辑您的相对布局实现

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

        <TextView 
         android:id="@+id/headerTxt" 
         style="@style/Base.TextAppearance.AppCompat.Medium" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:ellipsize="marquee" 
         android:layout_centerInParent="true" 
         android:maxLines="1" 
         android:text="Tile" 
         android:textColor="@android:color/black" /> 


        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src="@drawable/chat_new_icon" 
         android:layout_toRightOf="@+id/headerTxt" 
         android:layout_centerVertical="true" 
         /> 

       </RelativeLayout> 
+0

没有工作@MohdSaquib :( –

+0

你会给你的风格/ AppTheme.NoActionBar.PopupOverlay –

+0

请检查'