Android 多于一行的操作栏标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11259311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Action Bar Title with more than one line?
提问by benkap
Is it possible to create the title in the Action Bar with more than one line? I need a double spaced title.
是否可以在操作栏中创建多行标题?我需要一个双倍行距的标题。
回答by Thomas v d O
I am not quite sure if you were looking for the following, but:
我不太确定您是否正在寻找以下内容,但是:
actionBar.setSubtitle(String string) sets a second line with smaller font underneath your main Title.
actionBar.setSubtitle(String string) 在主标题下方设置第二行,字体较小。
example:
例子:
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());
}
回答by Matthias Robbers
I found a better solution that works perfectly up to Android 4.2.2 where the icon and the title are one clickable item.
我找到了一个更好的解决方案,它可以完美运行到 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);
}
If you are using ActionBarSherlock, use this code:
如果您使用的是 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);
}
回答by Mr-IDE
The default TextView on the ActionBar does not support line wrapping, and there is no exposed method to set it to wrap. So create a flexible layout, like this: action_bar_title_layout.xml:
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"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="center_vertical"
android:ellipsize="end"
android:maxLines="2"/>
</LinearLayout>
Then set this as the custom layout on your ActionBar:
然后将其设置为 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.");
回答by CommonsWare
No, but you can replace the icon with a separate logo graphic (android:logo
and then setDisplayUseLogoEnabled()
). You could embed your two-line title as part of the logo graphic and then not show the original title string via setDisplayShowTitleEnabled()
. AFAIK, that combination should work.
不可以,但您可以使用单独的徽标图形(android:logo
然后setDisplayUseLogoEnabled()
)替换该图标。您可以将两行标题作为徽标图形的一部分嵌入,然后不通过setDisplayShowTitleEnabled()
. AFAIK,这种组合应该有效。