Android 自定义工具栏视图未居中
将Android 自定义工具栏视图未居中,android,xml,toolbar,Android,Xml,Toolbar,将ActionBarDrawerToggle与我的自定义工具栏一起使用时,工具栏中的文本视图不再居中 main_layout.xml <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/dra
ActionBarDrawerToggle
与我的自定义工具栏
一起使用时,工具栏
中的文本视图
不再居中
main_layout.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:fitsSystemWindows="true" />
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<TextView
android:id="@+id/tvNavTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBackgroundBlack"
android:gravity="center"
android:textColor="@color/colorWhite"
android:textSize="@dimen/text_size_large" />
<TextView
android:id="@+id/tvNavDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBackgroundBlack"
android:gravity="center"
android:textColor="@color/colorWhite"
android:textSize="@dimen/text_size_small" />
</LinearLayout>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
我尝试设置
工具栏上的contentInsertStart
和其他属性,但没有任何更改。这里的问题是操作栏的图标设置为工具栏上的导航按钮。此按钮是工具栏
的特殊子按钮,在布局中优先。添加到工具栏
的任何其他子视图
将仅分配放置图像按钮
后剩余的空间。这会将相对视图的左侧推到右侧,因此居中的文本视图将不会相对于工具栏本身居中
幸运的是,Toolbar
的LayoutParams
具有重力属性,我们可以利用该属性将LinearLayout
及其TextView
直接放在Toolbar
中居中,而不必将它们包装在另一个视图组中。我们还可以在ImageView
上适当设置重力,以类似地将重力与右侧对齐
在本例中,我们通过将LinearLayout
的layout\u gravity
设置为center
来应用该重心。请务必将布局宽度
值更改为包裹内容
,否则您将和以前一样处境艰难。此处的ImageView
将其layout\u重力设置为右侧|中心(垂直)
,以替换那些layout*
特定于RelativeLayout
的属性
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tvNavTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite"
android:textSize="@dimen/text_size_large" />
<TextView
android:id="@+id/tvNavDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorWhite"
android:textSize="@dimen/text_size_small" />
</LinearLayout>
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_gravity="right|center_vertical"
android:src="@mipmap/ic_launcher" />
</android.support.v7.widget.Toolbar>
我也有同样的问题,我用android:contentInset
请尝试使用以下代码:
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:contentInsetEnd="50dp"
android:contentInsetLeft="50dp"
android:contentInsetRight="50dp"
android:contentInsetStart="50dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetEnd="50dp"
app:contentInsetLeft="50dp"
app:contentInsetRight="50dp"
app:contentInsetStart="50dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_centerInParent="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:text="@string/app_name_short"
android:textColor="#fff"
android:textSize="20dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/app_bar_layout" />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
到底是什么问题?你忘了提它了。空间本欣汉堡图标。我需要以全宽显示自定义工具栏工具栏
为全宽。这就是汉堡包的图标所在;工具栏
。那么为什么“主页”文本不在中间呢?因为RelativeLayout
在图标之后启动,图标本身就是一个图像按钮
。您的TextView
s位于RelativeLayout
的中心。