我希望在操作栏中心显示标题,并在其旁边放置操作图标(并忽略它们对重力设置的影响)。
期望的效果如下图所示:
而实际效果却是这样的:
这是我的代码:
所以我的问题是,标题居中只有在旁边没有操作栏项的情况下才起作用,一旦我添加一个项目,中心对齐就会出问题。如果我能够与它一起使用向上箭头按钮将会更好(现在这也会破坏居中)。任何帮助将不胜感激。我不想实现自己的操作栏项/按钮逻辑。
期望的效果如下图所示:
![enter image description here](https://istack.dev59.com/psJEK.webp)
![enter image description here](https://istack.dev59.com/DaQdz.webp)
ActionBar actionBar = getSupportActionBar();
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bar));
actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null), params);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="8dp"
android:src="@drawable/launcher_icon" />
<TextView
android:id="@+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:maxHeight="1dp"
android:maxLines="1"
android:paddingLeft="48dp"
android:paddingRight="48dp"
android:text="Bacon ipsum"
android:textColor="#777777"
android:textSize="20sp" />
</RelativeLayout>
所以我的问题是,标题居中只有在旁边没有操作栏项的情况下才起作用,一旦我添加一个项目,中心对齐就会出问题。如果我能够与它一起使用向上箭头按钮将会更好(现在这也会破坏居中)。任何帮助将不胜感激。我不想实现自己的操作栏项/按钮逻辑。