2017-07-04 46 views
-4

为什么在工具栏中的图像不能在中心对齐时删除android中的菜单项。
如果我隐藏了菜单项,标题右移,即在导航图标和标题之间添加了一些空格。工具栏中的标题图像对齐方式android

我的代码

<android.support.v7.widget.Toolbar 
android:id="@+id/toolbar" 
android:layout_width="match_parent" 
android:layout_height="?attr/actionBarSize" 
app:contentInsetLeft="0dp" 
app:contentInsetStart="0dp" 
app:contentInsetRight="0dp" 
app:contentInsetStartWithNavigation="0dp" 
android:background="#FFF"> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 
<ImageView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="5dp" 
android:id="@+id/pg_title" 
android:layout_centerHorizontal="true" 
android:src="@drawable/logo" 
/> 
</LinearLayout> 
</android.support.v7.widget.Toolbar> 
+0

请把你的代码在这里,我们可以看到你在做什么,并能帮助之后你会以更好的方式。 –

回答

1

尝试这种添加此工具栏布局XML文件

<android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <include layout="@layout/actionbar_layout" /> 

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

创建一个新的actionbar_layout为您的工具栏这样

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:orientation="horizontal"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_bike" /> 
</LinearLayout> 
现在

在活动文件中只需添加以下代码

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle(""); 

问我任何查询的情况下

+0

谢谢你的回复是的,我也加入了工具栏,就像你的代码一样。我已经使用fragements的第一个片段的工具栏有菜单项,所以标题正确对齐中心。当我删除或设置菜单项不可见。标题不对齐中心。 – Tamil