Android 在工具栏上居中显示标题
我有一个问题,我无法在我的工具栏中居中放置标题(@+id/Toolbar\u title)。我试着用一个相对的布局,使用重力:中心,但它不起作用。我已经添加了颜色来区分布局,但我还不能发布图片 活动\u main.xmlAndroid 在工具栏上居中显示标题,android,layout,center,toolbar,Android,Layout,Center,Toolbar,我有一个问题,我无法在我的工具栏中居中放置标题(@+id/Toolbar\u title)。我试着用一个相对的布局,使用重力:中心,但它不起作用。我已经添加了颜色来区分布局,但我还不能发布图片 活动\u main.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@android:color/holo_green_dark"
app:theme="@style/ToolbarTheme"
app:popupTheme="@style/Theme.AppCompat"
android:layout_gravity="center_vertical"
>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@color/primaryDark"
android:textSize="20dp"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/primary">
<ImageButton
android:id="@+id/show_list_actualites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_fleche_bas"
android:adjustViewBounds="true"
android:layout_toLeftOf="@+id/loupe"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:padding="15dp"
android:background="@color/primaryDark"
/>
<!--android:background="@android:color/transparent"-->
<ImageView
android:id="@+id/loupe"
android:layout_width="wrap_content"
android:layout_height="12dip"
android:src="@drawable/ic_rech"
android:rotation="270"
android:adjustViewBounds="true"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:background="@color/primaryDark"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/slide_news_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<ListView
android:id="@+id/list_slider_news_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/action_bar_background"
android:text="Item 1"/>
<Button
android:id="@+id/close_news_slide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_tranparent"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:background="@android:color/white"
android:layout_width="305dp"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
</style>
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="ToolbarTheme" parent="Theme.AppCompat">
<item name="android:windowNoTitle">true</item>
<item name="android:textColorPrimary">@color/action_bar_text</item>
<item name="actionMenuTextColor">@color/action_bar_text</item>
<item name="android:textColorSecondary">@color/action_bar_text</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
</resources>
styles.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@android:color/holo_green_dark"
app:theme="@style/ToolbarTheme"
app:popupTheme="@style/Theme.AppCompat"
android:layout_gravity="center_vertical"
>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@color/primaryDark"
android:textSize="20dp"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/primary">
<ImageButton
android:id="@+id/show_list_actualites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_fleche_bas"
android:adjustViewBounds="true"
android:layout_toLeftOf="@+id/loupe"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:padding="15dp"
android:background="@color/primaryDark"
/>
<!--android:background="@android:color/transparent"-->
<ImageView
android:id="@+id/loupe"
android:layout_width="wrap_content"
android:layout_height="12dip"
android:src="@drawable/ic_rech"
android:rotation="270"
android:adjustViewBounds="true"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:background="@color/primaryDark"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</RelativeLayout>
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:id="@+id/slide_news_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<ListView
android:id="@+id/list_slider_news_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/action_bar_background"
android:text="Item 1"/>
<Button
android:id="@+id/close_news_slide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/grey_tranparent"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<ListView
android:id="@+id/left_drawer"
android:background="@android:color/white"
android:layout_width="305dp"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primaryDark</item>
</style>
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="ToolbarTheme" parent="Theme.AppCompat">
<item name="android:windowNoTitle">true</item>
<item name="android:textColorPrimary">@color/action_bar_text</item>
<item name="actionMenuTextColor">@color/action_bar_text</item>
<item name="android:textColorSecondary">@color/action_bar_text</item>
<item name="android:layout_gravity">center_horizontal</item>
</style>
</resources>
真的
假的
@颜色/原色
@颜色/原色暗
真的
@颜色/动作\u栏\u文本
@颜色/动作\u栏\u文本
@颜色/动作\u栏\u文本
水平中心
试试这段代码
工具栏的父级是视图组。所以你不能这样设计。我使用FrameLayout来放置项目
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/primary">
<ImageButton
android:id="@+id/show_list_actualites"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:adjustViewBounds="true"
android:layout_toLeftOf="@+id/loupe"
android:layout_marginLeft="20dp"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:padding="15dp"
android:background="@color/primaryDark"
/>
<!-- android:background="@android:color/transparent"-->
<ImageView
android:id="@+id/loupe"
android:layout_width="wrap_content"
android:layout_height="12dip"
android:src="@drawable/ic_launcher"
android:rotation="270"
android:adjustViewBounds="true"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:background="@color/primaryDark"
/>
</RelativeLayout>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@color/primaryDark"
android:textSize="20dp"
android:text="Title"
/>
</FrameLayout>
将工具栏中的文本居中是违反Android设计标准的。我认为这就是为什么你在完成这项任务时遇到困难的原因。我不会尝试调整你的文本对齐方式,这将确保你的应用程序适合Android生态系统 有关工具栏设计标准的详细信息,请查看此链接:
我不是在回答你将如何做你所要求的,而是建议你首先不要这样做(使用标准格式,这对你来说会更容易)。你知道工具栏的标准是左对齐的吗?有一个原因是很难做到你正在尝试的(这不是设计标准)我想知道我是否应该使用我看不到的工具栏的基本标题?你看不到是什么意思?我被迫使用工具栏的标题使其居中?添加标题后,我不使用getSupportActionBar()setTitle(mTitle)。Just:Toolbar-toolbarTop=(Toolbar)findviewbyd(R.id.Toolbar);TextView tle=(TextView)toolbarTop.findViewById(R.id.toolbar\u title);tle.setText(标题);不,您缺少这一点,工具栏标题不应该居中,因此您应该使用工具栏而不是将文本居中。这对你来说很难,因为你不应该这么做。真奇怪,RelativeLayout没有占据整个工具栏,尽管match_parent:这个答案不应该被否决。仔细检查这些材料规格,它将确认我所说的是准确的-你不应该这样做(根据标准)