简体   繁体   中英

How do I center a logo in the ActionBar?

I am trying to center a logo in the MainActivity ActionBar but when I add this

getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.thelogo);
getSupportActionBar().setDisplayUseLogoEnabled(true);

it looks like this在此处输入图片说明

Is there no way to center it without having to change my RelativeLayout to a LinearLayout?

Since you can't access the view you can't center it unless there's a function for that which I don't think there is that's why I always make my own toolbars use this:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/navigation_background"
        android:layout_gravity="center"/>
</androidx.appcompat.widget.Toolbar>

in your code use now:

...
setSupportActionBar(toolbar);

Make sure you get rid of the default actionbar by changing style to:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM