【问题标题】:Action Bar Title with more than one line?多行的操作栏标题?
【发布时间】:2021-04-02 16:49:33
【问题描述】:

是否可以在操作栏中创建多行标题?我需要一个双倍行距的标题。

【问题讨论】:

    标签: android android-actionbar


    【解决方案1】:

    我不太确定您是否正在寻找以下内容,但是:

    actionBar.setSubtitle(String string) 在主标题下方设置第二行,字体较小。

    示例:

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_left_drawer_item_clicked);
            String value = getIntent().getExtras().getString("drawerItemString");
    
            ActionBar actionBar = getActionBar();
            // Make sure we're running on Honeycomb or higher to use ActionBar APIs
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                // Show the Up button in the action bar.
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
            actionBar.setTitle(selectedBook.getTitle());
            actionBar.setSubtitle(selectedBook.getAuthorName());
    
    }
    

    【讨论】:

    • 此处列出的最佳解决方案。应该被标记为答案!
    • 这个解决方案的问题是,如果 authorName 为空,标题仍然会出现在第一行,而第二行仍然是空的。那个样子不好看……
    • @AndreiF 如果是这种情况,您只需要设置一个条件来评估 authorName 是否为空。如果为空,则不执行 setSubtitle。
    【解决方案2】:

    我找到了一个更好的解决方案,它在 Android 4.2.2 中完美运行,其中图标和标题是一个可点击的项目。

    int titleId = Resources.getSystem()
            .getIdentifier("action_bar_title", "id", "android");
    if (titleId > 0) {
        TextView title = (TextView) findViewById(titleId);
        title.setSingleLine(false);
        title.setMaxLines(2);
        title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    }
    

    如果您使用的是 ActionBarSherlock,请使用以下代码:

    int titleId = Resources.getSystem()
            .getIdentifier("action_bar_title", "id", "android");
    if (titleId == 0) {
        titleId = com.actionbarsherlock.R.id.abs__action_bar_title;
    }
    TextView title = (TextView) findViewById(titleId);
    if (title != null) {
        title.setSingleLine(false);
        title.setMaxLines(2);
        title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    }
    

    【讨论】:

    • 这也可以使用支持库的 ActionBar 直到 4.4。但在平台 上失败
    【解决方案3】:

    ActionBar 上的默认 TextView 不支持换行,也没有暴露的方法设置为换行。所以创建一个灵活的布局,像这样:action_bar_title_layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    <TextView
        android:id="@+id/action_bar_title"
        style="@style/TextAppearance.AppCompat.Title"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:gravity="center_vertical"
        android:ellipsize="end"
        android:maxLines="2"/>
    
    </LinearLayout>
    

    然后将其设置为 ActionBar 上的自定义布局:

    ActionBar actionBar = getActionBar();
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setCustomView(R.layout.action_bar_title_layout);
    ((TextView) findViewById(R.id.action_bar_title)).setText(
        "This is a long text title that will wrap to multiple lines, when necessary.");
    

    【讨论】:

      【解决方案4】:

      不可以,但您可以将图标替换为单独的徽标图形(android:logo,然后是 setDisplayUseLogoEnabled())。您可以将两行标题作为徽标图形的一部分嵌入,然后通过setDisplayShowTitleEnabled() 不显示原始标题字符串。 AFAIK,这种组合应该有效。

      【讨论】:

      猜你喜欢
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-27
      • 2012-10-14
      • 2012-07-14
      • 1970-01-01
      相关资源
      最近更新 更多