在 android 中更改 CollapsingToolbarLayout 中工具栏标题的字体大小
Change font size of Toolbar title in CollapsingToolbarLayout in android
我在 android.support.design.widget.CollapsingToolbarLayout 中有一个 Toolbar(android.support.v7.widget.Toolbar),我想更改工具栏标题的字体大小。我试过 app:titleTextAppearance 和一些较早的建议,但它们不起作用。我的代码如下。
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/route_detail_image"
android:src="@drawable/default_image"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="90dip"
android:layout_gravity="bottom"
android:padding="10dip"
android:background="@color/trans_black"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:elevation="20dp"
app:titleTextAppearance="@style/route_title"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
在我的 style.xml
里面
<style name="route_title" parent="android:Widget.TextView">
<item name="android:textSize">14sp</item>
<item name="android:color">#ffff0000</item>
</style>
里面。java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(route_name);
我想你需要这个:
try
{
final Field field = collapsingToolbarLayout.getClass().getDeclaredField("mCollapsingTextHelper");
field.setAccessible(true);
final Object object = field.get(collapsingToolbarLayout);
final Field tpf = object.getClass().getDeclaredField("mTextPaint");
tpf.setAccessible(true);
((TextPaint) tpf.get(object)).setTextSize(getResources().getDimension(R.dimen.textsize));
}
catch (Exception ex) {
}
您可以使用 addView()
将自定义布局设置为您想要的大小 Toolbar
得到答案
mCollapsingToolbarLayout.setTitle(getTitle());
mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
<style name="ExpandedAppBar" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">28sp</item>
<item name="android:textColor">#000</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsedAppBar" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">24sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textStyle">normal</item>
</style>
<style name="ExpandedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">28.5sp</item>
<item name="android:textColor">#000</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">24.5sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textStyle">normal</item>
</style>
我在 android.support.design.widget.CollapsingToolbarLayout 中有一个 Toolbar(android.support.v7.widget.Toolbar),我想更改工具栏标题的字体大小。我试过 app:titleTextAppearance 和一些较早的建议,但它们不起作用。我的代码如下。
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/route_detail_image"
android:src="@drawable/default_image"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="90dip"
android:layout_gravity="bottom"
android:padding="10dip"
android:background="@color/trans_black"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:elevation="20dp"
app:titleTextAppearance="@style/route_title"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.CollapsingToolbarLayout>
在我的 style.xml
里面<style name="route_title" parent="android:Widget.TextView">
<item name="android:textSize">14sp</item>
<item name="android:color">#ffff0000</item>
</style>
里面。java
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(route_name);
我想你需要这个:
try
{
final Field field = collapsingToolbarLayout.getClass().getDeclaredField("mCollapsingTextHelper");
field.setAccessible(true);
final Object object = field.get(collapsingToolbarLayout);
final Field tpf = object.getClass().getDeclaredField("mTextPaint");
tpf.setAccessible(true);
((TextPaint) tpf.get(object)).setTextSize(getResources().getDimension(R.dimen.textsize));
}
catch (Exception ex) {
}
您可以使用 addView()
Toolbar
mCollapsingToolbarLayout.setTitle(getTitle());
mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
<style name="ExpandedAppBar" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">28sp</item>
<item name="android:textColor">#000</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsedAppBar" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">24sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textStyle">normal</item>
</style>
<style name="ExpandedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">28.5sp</item>
<item name="android:textColor">#000</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">24.5sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textStyle">normal</item>
</style>