居中显示带有按钮的操作栏标题

4
我希望在操作栏中心显示标题,并在其旁边放置操作图标(并忽略它们对重力设置的影响)。
期望的效果如下图所示: enter image description here 而实际效果却是这样的: enter image description here 这是我的代码:
    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>

所以我的问题是,标题居中只有在旁边没有操作栏项的情况下才起作用,一旦我添加一个项目,中心对齐就会出问题。如果我能够与它一起使用向上箭头按钮将会更好(现在这也会破坏居中)。任何帮助将不胜感激。我不想实现自己的操作栏项/按钮逻辑。
3个回答

3
您可以在TextView中尝试这个:
<TextView
    android:id="@+id/actionbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:maxHeight="1dp"
    android:maxLines="1"
    android:paddingLeft="48dp"
    android:paddingRight="48dp"
    android:text="Bacon ipsum"
    android:textColor="#777777"
    android:textSize="20sp" />

1
很遗憾,我不能只设置填充并将其保留在那里,我必须根据右侧是否有操作项来不断开启和关闭它。哦,这比自己实现按钮要好一些,所以我认为这将是答案。谢谢。 - Tamas

2
  1. Remove the launcher icon from your RelativeLayout.
  2. Change the width of the RelativeLayout to wrap_content
  3. Remove:

    actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false);

  4. Add:

    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(
    
    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
    
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
    actionBar.setCustomView(yourInflatedCustomView, lp);
    

您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Amokrane Chentir
这对我也起作用。难以置信......我花费了很多时间。干杯 - blub

0

你尝试过将这段代码实现到文本中吗?

android:layout_centerHorizontal="true"

问题在于,操作项占据了自定义视图旁边的空间,因此即使我在布局中居中它,我仍然需要像user2203377建议的那样调整填充。 - Tamas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接