mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-04 13:53:16 +08:00
[CollapsingToolbarLayout] Added experimental API to allow generic customizations of the StaticLayout.Builder used for the title text
Example usage:
```
collapsingToolbarLayout.setStaticLayoutBuilderConfigurer(
builder ->
builder.setLineBreakConfig(
new LineBreakConfig.Builder()
.setLineBreakWordStyle(LineBreakConfig.LINE_BREAK_WORD_STYLE_PHRASE)
.build()));
```
PiperOrigin-RevId: 455625977
This commit is contained in:
parent
0cb355b26a
commit
241aa5cd8b
@ -1398,6 +1398,24 @@ public class CollapsingToolbarLayout extends FrameLayout {
|
||||
return collapsingTextHelper.getHyphenationFrequency();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link StaticLayoutBuilderConfigurer} for the {@link android.text.StaticLayout} of the
|
||||
* title text.
|
||||
*
|
||||
* <p>Note that the updates to the {@link android.text.StaticLayout.Builder} in the configure
|
||||
* method (e.g., the text, alignment, max lines, line spacing, etc.) will take precedence over and
|
||||
* overwrite any previous configurations made by the {@link CollapsingToolbarLayout} to the same
|
||||
* properties.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@RestrictTo(LIBRARY_GROUP)
|
||||
@RequiresApi(VERSION_CODES.M)
|
||||
public void setStaticLayoutBuilderConfigurer(
|
||||
@Nullable StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer) {
|
||||
collapsingTextHelper.setStaticLayoutBuilderConfigurer(staticLayoutBuilderConfigurer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets whether {@code TextDirectionHeuristics} should be used to determine whether the title text
|
||||
* is RTL. Experimental Feature.
|
||||
@ -1732,4 +1750,15 @@ public class CollapsingToolbarLayout extends FrameLayout {
|
||||
collapsingTextHelper.setExpansionFraction(Math.abs(verticalOffset) / (float) expandRange);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Interface that allows for further customization of the provided {@link
|
||||
* android.text.StaticLayout}.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@RestrictTo(LIBRARY_GROUP)
|
||||
@RequiresApi(VERSION_CODES.M)
|
||||
public interface StaticLayoutBuilderConfigurer
|
||||
extends com.google.android.material.internal.StaticLayoutBuilderConfigurer {}
|
||||
}
|
||||
|
||||
@ -176,6 +176,7 @@ public final class CollapsingTextHelper {
|
||||
private float lineSpacingAdd = StaticLayoutBuilderCompat.DEFAULT_LINE_SPACING_ADD;
|
||||
private float lineSpacingMultiplier = StaticLayoutBuilderCompat.DEFAULT_LINE_SPACING_MULTIPLIER;
|
||||
private int hyphenationFrequency = StaticLayoutBuilderCompat.DEFAULT_HYPHENATION_FREQUENCY;
|
||||
@Nullable private StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer;
|
||||
|
||||
public CollapsingTextHelper(View view) {
|
||||
this.view = view;
|
||||
@ -1075,6 +1076,7 @@ public final class CollapsingTextHelper {
|
||||
.setMaxLines(maxLines)
|
||||
.setLineSpacing(lineSpacingAdd, lineSpacingMultiplier)
|
||||
.setHyphenationFrequency(hyphenationFrequency)
|
||||
.setStaticLayoutBuilderConfigurer(staticLayoutBuilderConfigurer)
|
||||
.build();
|
||||
} catch (StaticLayoutBuilderCompatException e) {
|
||||
Log.e(TAG, e.getCause().getMessage(), e);
|
||||
@ -1220,6 +1222,15 @@ public final class CollapsingTextHelper {
|
||||
return hyphenationFrequency;
|
||||
}
|
||||
|
||||
@RequiresApi(VERSION_CODES.M)
|
||||
public void setStaticLayoutBuilderConfigurer(
|
||||
@Nullable StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer) {
|
||||
if (this.staticLayoutBuilderConfigurer != staticLayoutBuilderConfigurer) {
|
||||
this.staticLayoutBuilderConfigurer = staticLayoutBuilderConfigurer;
|
||||
recalculate(/* forceRecalculate= */ true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
|
||||
* defined as it's difference being < 0.00001.
|
||||
|
||||
@ -84,6 +84,7 @@ final class StaticLayoutBuilderCompat {
|
||||
private boolean includePad;
|
||||
private boolean isRtl;
|
||||
@Nullable private TextUtils.TruncateAt ellipsize;
|
||||
@Nullable private StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer;
|
||||
|
||||
private StaticLayoutBuilderCompat(CharSequence source, TextPaint paint, int width) {
|
||||
this.source = source;
|
||||
@ -219,6 +220,17 @@ final class StaticLayoutBuilderCompat {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the {@link StaticLayoutBuilderConfigurer} which allows additional custom configurations on
|
||||
* the static layout.
|
||||
*/
|
||||
@NonNull
|
||||
public StaticLayoutBuilderCompat setStaticLayoutBuilderConfigurer(
|
||||
@Nullable StaticLayoutBuilderConfigurer staticLayoutBuilderConfigurer) {
|
||||
this.staticLayoutBuilderConfigurer = staticLayoutBuilderConfigurer;
|
||||
return this;
|
||||
}
|
||||
|
||||
/** A method that allows to create a StaticLayout with maxLines on all supported API levels. */
|
||||
public StaticLayout build() throws StaticLayoutBuilderCompatException {
|
||||
if (source == null) {
|
||||
@ -259,6 +271,9 @@ final class StaticLayoutBuilderCompat {
|
||||
if (maxLines > 1) {
|
||||
builder.setHyphenationFrequency(hyphenationFrequency);
|
||||
}
|
||||
if (staticLayoutBuilderConfigurer != null) {
|
||||
staticLayoutBuilderConfigurer.configure(builder);
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2022 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.android.material.internal;
|
||||
|
||||
import android.text.StaticLayout;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.RestrictTo;
|
||||
import androidx.annotation.RestrictTo.Scope;
|
||||
|
||||
/**
|
||||
* Interface that allows for further customization of the provided {@link
|
||||
* android.text.StaticLayout}.
|
||||
*
|
||||
* @hide
|
||||
*/
|
||||
@RestrictTo(Scope.LIBRARY_GROUP)
|
||||
public interface StaticLayoutBuilderConfigurer {
|
||||
void configure(@NonNull StaticLayout.Builder builder);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user