【问题标题】:Actionbar centered title with buttons带有按钮的以操作栏为中心的标题
【发布时间】:2015-05-19 22:20:43
【问题描述】:

我想将标题置于操作栏中的中心,同时在其旁边放置操作图标(并在重力设置中忽略它们)。

我想要这个:

相反,我有这个:

这是我的代码:

    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>

所以我的问题是,只有标题居中才有效,如果我旁边有 0 个操作栏项目,只要我添加一个项目,居中就会搞砸。 如果我可以使用向上提示按钮(现在也搞砸了居中),那也很棒。

任何帮助将不胜感激。

我不想实现自己的操作栏项/按钮逻辑。

【问题讨论】:

    标签: android android-actionbar actionbarsherlock


    【解决方案1】:

    你可以在 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" />
    

    【讨论】:

    • 遗憾的是,我不能只设置填充并将其留在那里,我必须不断打开和关闭它,这取决于它的右侧是否有操作项。哦,好吧,这比我自己实现按钮更糟糕,所以我认为这就是答案。谢谢。
    【解决方案2】:
    1. 从您的 RelativeLayout 中删除启动器图标。
    2. 将RelativeLayout的宽度改为wrap_content
    3. 删除:

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

    4. 添加:

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

    【讨论】:

    • 这应该是公认的答案。 Tamas,你应该给它一个机会 :) 只是缺少一行:ActionBar#setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)。
    • 它也适用于我。难以置信...我为此付出了代价。欢呼
    【解决方案3】:

    您是否尝试过将这段代码实现到文本中?

    android:layout_centerHorizontal="true"
    

    【讨论】:

    • 问题是,操作项占用了自定义视图旁边的空间,所以即使我在布局中居中,我仍然需要使用填充,就像 user2203377 建议的那样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 2021-12-15
    • 2013-02-23
    • 2018-04-13
    • 1970-01-01
    相关资源
    最近更新 更多