3
我有一个工具栏,当在横向模式下的宽度不像平时那么宽,但它的高度比平时多,因此我想将标题设置为多行(2行精确)当它在风景。我尝试了一些将TextView放入工具栏的东西,但是当我尝试访问TextView以设置其标题(因为标题是可变的)时,我使用findViewById后得到了一个NullPointer。Android多行工具栏标题
这是当前形势的一些代码(无多):
@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" />
我真的没有一个明确的字幕,但只是一个很长的标题。设置一个字幕会涉及到使用2个不同的字符串(1为标题,1为字幕),但我只有一个字符串,所以我不明白这有什么帮助。 –
@DennisvanOpstal查看我更新的答案。 – Ironman
如果我使用更新的答案,我是否会以相同的方式设置标题,还是必须以不同的方式进行设置? –