Android多行工具栏标题 [英] Android Multiline Toolbar Title
问题描述
我有一个工具栏,当它在横向模式下不像平常那样宽,但它的高度比平常高,因此我想将标题设置为多行(准确地说是2行)在景观中。我尝试了一些将TextView放在工具栏中的东西,但是当我尝试访问TextView来设置它的标题时(因为标题是可变的)我在使用findViewById后得到一个NullPointer。
I have a Toolbar that when in landscape mode isn't as wide as usual, but it has more height than usual and for that reason I want to set the title to be multiline (2 lines to be precise) when it is in landscape. I have tried some things where I put a TextView inside of the Toolbar, but when I try to access the TextView to set it's title (since the the title is variable) I get a NullPointer after using findViewById.
以下是当前情况的一些代码(没有多行):
Here is some code of the current situation (without multiline):
@Override
protected void onResume() {
super.onResume();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = (int)( size.x * 0.37);
int height = Math.round(size.x * 0.63f * 0.25f);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
toolbar.setLayoutParams(params);
}
if (executeOnResume) {
currentProject = getIntent().getParcelableExtra("Project");
getSupportActionBar().setTitle("This is a pretty long piece of text, I hope it fits");
// A lot of irrelevant code...
//...
//...
} else { loadStuff();}
}
这是工具栏xml:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:title="@string/menuLabel1a"
app:layout_widthPercent="37%"
app:layout_heightPercent="26%"
android:gravity="top"
app:titleTextAppearance="@style/toolbarTitleText"
android:background="@color/greyLight"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay" />
推荐答案
试试这个:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="@dimen/double_height_toolbar"
android:gravity="top"
app:titleTextAppearance="@style/toolbarTitleText"
android:background="@color/greyLight"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay">
<TextView
android:id="@+id/product_details_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:title="@string/menuLabel1a"
android:paddingTop="@dimen/padding_normal"
style="@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse"
android:maxLines="2" />
</android.support.v7.widget.Toolbar>
其他解决方案适用于ActionBar
ActionBar的默认TextView不支持换行,并且没有公开的方法将其设置为换行。因此,您可以创建灵活的布局,如下所示: action_bar_title_layout.xml :
The default TextView of the ActionBar does not support line wrapping, and there is no exposed method to set it to wrap. So you can create a flexible layout, like this: 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>
然后将其设置为ActionBar上的自定义布局:
Then set this as the custom layout on your 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.");
这篇关于Android多行工具栏标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!