2016-06-08 33 views
0

我創建了一個自定義工具欄,圖像作爲中心標題,另一個對齊右側。我使用了相對佈局來放置圖像瀏覽。但是相對佈局似乎無法覆蓋工具欄的整個長度,儘管其寬度設置爲match_parent。代碼如下:在自定義工具欄中居中標題

activity_main.xml中

<android.support.v7.widget.Toolbar 
     android:id="@+id/app_bar" 
     android:minHeight="?attr/actionBarSize" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_width="match_parent" 
     android:background="?attr/colorPrimary"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      > 
      <ImageView 
       android:layout_width="150dp" 
       android:layout_height="30dp" 
       android:src="@drawable/title" 
       android:id="@+id/title_image" 
       android:layout_centerInParent="true" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/oie_transparent" 
       android:layout_alignParentRight="true" 
       android:id="@+id/cart_icon" 
       android:layout_toRightOf="@id/title_image" 
       android:layout_marginTop="2dp" 
       android:layout_marginLeft="30dp" 
       android:layout_marginBottom="2dp"/> 
     </RelativeLayout> 
    </android.support.v7.widget.Toolbar> 

MainActivity.java

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); // an example activity_main.xml is provided below 
     Toolbar tb = (Toolbar) findViewById(R.id.app_bar); 
     setSupportActionBar(tb); 
    } 
+0

看到這個http://stackoverflow.com/questions/26533510/android-toolbar-center-title-and-custom-font – Meenaxi

+0

它不包括相對佈局和多個孩子 –

回答

0

工具欄軸承左,右必須徹底證明,否則仍然是一個5DP像一本打開左側。以下代碼:img左對齊,右對齊img標題已居中。

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbarhome" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    app:popupTheme="@style/AppTheme.PopupOverlay" 
    app:titleTextAppearance="@style/Toolbar.TitleText" 
    app:layout_collapseMode="pin" 
    android:background="@color/colorPrimary" 
    android:contentInsetLeft="0dp" 
    android:contentInsetStart="0dp" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    android:contentInsetRight="0dp" 
    android:contentInsetEnd="0dp" 
    app:contentInsetRight="0dp" 
    app:contentInsetEnd="0dp" > 

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="4" 
      android:gravity="center|left"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:paddingLeft="@dimen/bes" 
       android:src="@mipmap/ic_launcher" /> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="2" 
      android:gravity="center"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Title" 
       android:textColor="@color/beyaz" 
       android:textStyle="bold" 
       android:textSize="17sp" 
       android:gravity="center" 
       android:id="@+id/actiontitle"/> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="4" 
      android:gravity="center|right"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@mipmap/msr" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:paddingRight="@dimen/bes" /> 

     </RelativeLayout> 

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