Android layout Android工具栏的标题对齐问题
我第一次使用工具栏。在我的应用程序中,我将其用作操作栏 我想将工具栏的标题与中心对齐。但是我意识到没有内置的方法可以让你这么做 因此,我在工具栏中添加了一个textview,如下所示-Android layout Android工具栏的标题对齐问题,android-layout,android-toolbar,Android Layout,Android Toolbar,我第一次使用工具栏。在我的应用程序中,我将其用作操作栏 我想将工具栏的标题与中心对齐。但是我意识到没有内置的方法可以让你这么做 因此,我在工具栏中添加了一个textview,如下所示- <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:minHeight="?attr/actionBarSize" android:background="#
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
**android:gravity="center"**
android:id="@+id/txtTitle"
android:textColor="@android:color/white"
android:text="My Title" />
</android.support.v7.widget.Toolbar>
对如何解决这个问题有什么建议吗
谢谢 只需添加与主图标宽度相同的边距即可。我想
android:layout_marginRight="56dp"
应该刚好合适。添加您自己的文本视图,该视图可以在任何需要的地方对齐:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@android:color/white"
android:text="My Title"
android:textStyle="bold"/>
添加边距确实在我脑海中浮现,但如何确保标题在所有设备上都居中对齐?只要边距与图标大小相同,这将在任何设备上都能完美工作。图标是56dp。但请记住,如果将菜单添加到工具栏,则需要相应地调整边距。还要注意的是,居中标题违背了设计准则。我使用你的方法,因为这似乎是最好的解决办法。顺便说一句,你从哪里得到的菜单图标大小为56dp?我不知道图标大小是在哪里定义的,但它是正方形的,工具栏高度是56dp。你一定要读书。