Android 多行工具栏标题
Android Multiline Toolbar Title
我有一个工具栏,在横向模式下它不像平时那么宽,但它比平时高,因此我想将标题设置为多行(准确地说是 2 行)它在风景中。我尝试了一些将 TextView 放入工具栏内的操作,但是当我尝试访问 TextView 以设置它的标题时(因为标题是可变的),我在使用 findViewById 后得到了一个 NullPointer。
这是当前情况的一些代码(没有多行):
@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:
<?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 上的自定义布局:
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.");
找到适合我的解决方案:
fun Toolbar.disableTitleSingleLine() {
for (i in 0..childCount) {
val child = getChildAt(i)
if (child is TextView) {
child.isSingleLine = false
}
}
}
根据 Toolbar
实施,标题 TextView
是在设置文本时创建的。
在设置标题后调用此扩展很重要。
我有一个工具栏,在横向模式下它不像平时那么宽,但它比平时高,因此我想将标题设置为多行(准确地说是 2 行)它在风景中。我尝试了一些将 TextView 放入工具栏内的操作,但是当我尝试访问 TextView 以设置它的标题时(因为标题是可变的),我在使用 findViewById 后得到了一个 NullPointer。
这是当前情况的一些代码(没有多行):
@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:
<?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 上的自定义布局:
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.");
找到适合我的解决方案:
fun Toolbar.disableTitleSingleLine() {
for (i in 0..childCount) {
val child = getChildAt(i)
if (child is TextView) {
child.isSingleLine = false
}
}
}
根据 Toolbar
实施,标题 TextView
是在设置文本时创建的。
在设置标题后调用此扩展很重要。