如何在ActionBar中居中徽标?

问题描述 投票:2回答:1

我试图在MainActivity ActionBar中居中一个徽标,但是当我添加它时

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

它看起来像这个enter image description here

有没有办法集中它而不必将RelativeLayout更改为LinearLayout?

java android android-studio kotlin android-actionbar
1个回答
2
投票

既然你无法访问视图,你就无法将它放在中心位置,除非有一个我认为不存在的函数,这就是为什么我总是让我自己的工具栏使用它:

<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>

在您的代码中立即使用:

...
setSupportActionBar(toolbar);

确保通过将样式更改为以下内容来删除默认操作栏:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
© www.soinside.com 2019 - 2024. All rights reserved.