Update the collapsing toolbar layout

- Updating the height of title area to fulfill the spec
- Increasing the trigger points of animation for fade mode based
  on the line count of title, so the overlapping problem is
  prevented from the title scrolling up the title area
- Removing getLineCountWithReflection method since
  CollapsingToolbarLayout has already been supporting this method

Fix: 188608451
Fix: 189068911
Test: visual verified
Change-Id: I8dac87458f13d79e1941a4f2b3e961ac4d6a2944
This commit is contained in:
Mill Chen
2021-05-27 17:44:00 +08:00
parent 018e114202
commit cb4bb08c5e
2 changed files with 14 additions and 33 deletions

View File

@@ -16,10 +16,12 @@
-->
<resources>
<!-- Collapsing toolbar layout dimensions -->
<dimen name="toolbar_one_line_height">226dp</dimen>
<dimen name="toolbar_two_lines_height">270dp</dimen>
<dimen name="toolbar_three_lines_height">314dp</dimen>
<dimen name="toolbar_one_line_height">216dp</dimen>
<dimen name="toolbar_two_lines_height">260dp</dimen>
<dimen name="toolbar_three_lines_height">304dp</dimen>
<dimen name="scrim_visible_height_trigger">174dp</dimen>
<dimen name="scrim_visible_height_trigger_two_lines">218dp</dimen>
<dimen name="scrim_visible_height_trigger_three_lines">262dp</dimen>
<dimen name="expanded_title_margin_start">24dp</dimen>
<dimen name="expanded_title_margin_end">24dp</dimen>
</resources>

View File

@@ -26,12 +26,9 @@ import androidx.annotation.Nullable;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* A customized version of CollapsingToolbarLayout that can apply different font size based on
* the line count of its title.
* A customized version of CollapsingToolbarLayout that can apply different font size based on the
* line count of its title.
*/
public class AdjustableToolbarLayout extends CollapsingToolbarLayout {
@@ -59,43 +56,25 @@ public class AdjustableToolbarLayout extends CollapsingToolbarLayout {
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
final int count = getLineCountWithReflection();
final int count = getLineCount();
if (count > TOOLBAR_MAX_LINE_NUMBER) {
final ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = getResources()
.getDimensionPixelSize(R.dimen.toolbar_three_lines_height);
setScrimVisibleHeightTrigger(
getResources().getDimensionPixelSize(
R.dimen.scrim_visible_height_trigger_three_lines));
setLayoutParams(lp);
} else if (count == TOOLBAR_MAX_LINE_NUMBER) {
final ViewGroup.LayoutParams lp = getLayoutParams();
lp.height = getResources()
.getDimensionPixelSize(R.dimen.toolbar_two_lines_height);
setScrimVisibleHeightTrigger(
getResources().getDimensionPixelSize(
R.dimen.scrim_visible_height_trigger_two_lines));
setLayoutParams(lp);
}
}
});
}
/**
* Returns a number of title line count for CollapsingToolbarLayout so that facilitates the
* determination to apply what kind of font size. Since the title of CollapsingToolbarLayout is
* drawn in a canvas and the text process is wrapped in a CollapsingTextHelper, the way we used
* here is to get the line count from the CollapsingTextHelper via Java Reflection.
*/
private int getLineCountWithReflection() {
try {
final Field textHelperField =
this.getClass().getSuperclass().getDeclaredField("collapsingTextHelper");
textHelperField.setAccessible(true);
final Object textHelperObj = textHelperField.get(this);
final Field layoutField = textHelperObj.getClass().getDeclaredField("textLayout");
layoutField.setAccessible(true);
final Object layoutObj = layoutField.get(textHelperObj);
final Method method = layoutObj.getClass().getDeclaredMethod("getLineCount");
return (int) method.invoke(layoutObj);
} catch (Exception e) {
return 0;
}
}
}