我试图将 Logo 置于 MainActivity ActionBar 的中心,但是当我添加此内容时
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.drawable.thelogo);
getSupportActionBar().setDisplayUseLogoEnabled(true);
有没有办法在不将我的RelativeLayout更改为LinearLayout的情况下使其居中?
最佳答案
由于您无法访问 View ,因此无法将其居中,除非有一个我认为不存在的功能,这就是为什么我总是让自己的工具栏使用它:
<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">
关于java - 如何使 Logo 在 ActionBar 中居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54609692/