How to center ImageView in a toolbar?

2019-04-19 01:04发布

Been trying to center a logo inside my toolbar. When i navigate to the next activity, the "up affordance" icon is present, and it will push my logo slightly to the right. How can I keep my logo at the center of the toolbar, without removing the up affordance icon?

This is my toolbar tag

<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:paddingTop="@dimen/app_bar_top_padding"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:theme="@style/CustomToolBarTheme">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_android_logo"
    android:layout_gravity="center"
    android:paddingBottom="3dp"/>
 </android.support.v7.widget.Toolbar>

8条回答
做个烂人
2楼-- · 2019-04-19 01:58

You can do programmatically, that is the only way I've managed to fully center a logo in the toolbar.

Add to you activity:

@Override
public void onWindowFocusChanged(boolean hasFocus){

    // set toolbar logo to center programmatically
    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    ImageView logo = (ImageView) findViewById(R.id.logo);
    int offset = (toolbar.getWidth() / 2) - (logo.getWidth() / 2);
    // set
    logo.setX(offset);

}

and your toolbar_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<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/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/bg_yellow"
    android:elevation="4dp"
    android:fontFamily="@string/font_family_thin"
    app:contentInsetStartWithNavigation="0dp"
    android:layout_gravity="center_horizontal">

    <TextView
        android:id="@+id/title_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@string/font_family_light"
        android:textColor="@color/text_dark_xxx"
        android:textSize="@dimen/text_default"
        android:layout_centerVertical="true"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/logo"
        android:id="@+id/logo"
        android:paddingTop="5dp" />



</android.support.v7.widget.Toolbar>
查看更多
爷的心禁止访问
3楼-- · 2019-04-19 02:01

Set the image as a background of the toolbar, not as a child view.

<android.support.v7.widget.Toolbar
    android:id="@+id/detail_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:background="@drawable/my_image"
    app:theme="@style/CustomToolBarTheme">
查看更多
登录 后发表回答