Android 自动调整TextView大小的着色器LinearGradient未按预期工作

Android 自动调整TextView大小的着色器LinearGradient未按预期工作,android,text,Android,Text,我有一个自动调整大小的TextView: <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http

我有一个自动调整大小的
TextView

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootView">

    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="bottom|center_horizontal"
        android:textAllCaps="true"
        android:textColor="#435f7b"
        android:maxLines="1"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="10sp"
        app:autoSizeMaxTextSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</android.support.constraint.ConstraintLayout>
但它显然使用了错误的维度来计算文本限制,因此它看起来是这样的(例如,在Android 6.0上):

布局用作片段,使用FragmentStatePagerAdapter和PagerAdapter

如何让它工作?它可以在其他
TextView
s集合上正常工作,无需自动调整大小,但只能在
textSize
中工作,也可以在
Fragment
s中工作


非常感谢

这是您根据EugenPechanec的评论寻找的代码

TextView textView = findViewById(R.id.my_text_view);
textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        int color1 = Color.RED; // Or whatever color you need.
        int color2 = Color.BLUE;
        TextView tv = (TextView) v;

        Shader shader = new LinearGradient(0, 0, 0, tv.getLineHeight() * 1.1f, color1, color2, Shader.TileMode.REPEAT);
        tv.getPaint().setShader(shader);
    }
});

您可能希望在每次线条高度更改时更新着色器。@EugenPechanec但这样做会面临性能问题吗?不管怎样,怎么做?没有像“onPostDraw”这样的方法可以在着色器内部调用?感谢Sautosizing会导致重新布局,所以我想到了
视图。addOnLayoutChangeListener
在这里,您可以设置着色器的新实例,并调用
invalidate
以使用新着色器重新绘制。@Eugenepechanec请用代码发布答案?请尝试使用
文本视图。getTextSize
而不是
textView.getLineHeight()*1.1f
或查看此帮助
TextView textView = findViewById(R.id.my_text_view);
textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        int color1 = Color.RED; // Or whatever color you need.
        int color2 = Color.BLUE;
        TextView tv = (TextView) v;

        Shader shader = new LinearGradient(0, 0, 0, tv.getLineHeight() * 1.1f, color1, color2, Shader.TileMode.REPEAT);
        tv.getPaint().setShader(shader);
    }
});