AutoSize属性问题探索

AutoSize属性问题探索AutoSize属性问题探索如有错误,欢迎指出Google在Android8.0(APIlevel26)中,为TextView加入了一个动态属性AutoSize。在布局文件中,直接设置autoSizeTextType属性为uniform即可。这样,文本内容便会忽略android:textSize属性,从水平和垂直两个方向上缩放文本的内容。android:autoSizeTextType=”uniform”android:autoSizeMaxT

大家好,又见面了,我是你们的朋友全栈君。

AutoSize属性问题探索

如有错误,欢迎指出

Google在Android 8.0(API level 26)中,为TextView加入了一个动态属性AutoSize。
在布局文件中,直接设置autoSizeTextType属性为uniform即可。这样,文本内容便会忽略android:textSize属性,从水平和垂直两个方向上缩放文本的内容。

            android:autoSizeTextType="uniform"
            android:autoSizeMaxTextSize="35dp"
            android:autoSizeStepGranularity="1dp"
            android:autoSizeMinTextSize="1dp"

在API26以下,实现AutoSize需要支持库

<!-- 引入命名空间 -->
xmlns:app="http://schemas.android.com/apk/res-auto"

            app:autoSizeTextType="uniform"
            app:autoSizeMaxTextSize="35dp"
            app:autoSizeStepGranularity="1dp"
            app:autoSizeMinTextSize="1dp"

最近在使用autoSize时遇到了几个坑:

  1. 不能与SingleLine一起用
    与single连用后,当文本过长时会显示省略号,改用maxLines=1即可

  2. RadioButton的autosize属性
    经过多次试验,支持库在某些安卓版本上好像无法实现RadioButton的autosize属性

API app android
25 NO NO
26 NO YES
28 YES YES

我们知道,在API26中为TextView引入了AutoSize属性,而RadioButton -> CompoundButton(接口) -> Button -> TextView,因此在API26+中通过android设置的autosize属性可以生效

现在来分析通过app设置的autosize属性
support v7中的RadioButton的完整类名为:android.support.v7.widget.AppCompatRadioButton

public class AppCompatRadioButton extends RadioButton implements TintableCompoundButton { 
   
    private final AppCompatCompoundButtonHelper mCompoundButtonHelper;
    private final AppCompatTextHelper mTextHelper;
    ......
    public AppCompatRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { 
   
        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
        this.mCompoundButtonHelper = new AppCompatCompoundButtonHelper(this);
        this.mCompoundButtonHelper.loadFromAttributes(attrs, defStyleAttr);
        this.mTextHelper = new AppCompatTextHelper(this);
        this.mTextHelper.loadFromAttributes(attrs, defStyleAttr);
    }
    ......
}

可以看到,AppCompatRadioButton中存在一个与text相关的成员变量mTextHelper

    AppCompatTextHelper(TextView view) { 
   
        this.mView = view;
        this.mAutoSizeTextHelper = new AppCompatTextViewAutoSizeHelper(this.mView);
    }

终于看到autosize了,现在去看看mAutoSizeTextHelper

class AppCompatTextViewAutoSizeHelper { 
   
    AppCompatTextViewAutoSizeHelper(TextView textView) { 
   
        this.mTextView = textView;
        this.mContext = this.mTextView.getContext();
    }
    void setAutoSizeTextTypeWithDefaults(int autoSizeTextType) { 
   ......}
    void setAutoSizeTextTypeUniformWithConfiguration(int autoSizeMinTextSize, int autoSizeMaxTextSize, int autoSizeStepGranularity, int unit) throws IllegalArgumentException { 
   ......}
    void setAutoSizeTextTypeUniformWithPresetSizes(@NonNull int[] presetSizes, int unit) throws IllegalArgumentException { 
   ......}
    ......    
}

可以发现 AppCompatTextViewAutoSizeHelper 类正是实现autosize属性的重要类,那么,是如何调用的呢?
我们再回到AppCompatRadioButton

    public AppCompatRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { 
   
        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);
        this.mCompoundButtonHelper = new AppCompatCompoundButtonHelper(this);
        this.mCompoundButtonHelper.loadFromAttributes(attrs, defStyleAttr);
        this.mTextHelper = new AppCompatTextHelper(this);
        this.mTextHelper.loadFromAttributes(attrs, defStyleAttr);
    }

通过搜索,mTextHelper仅出现在构造函数中
我们来看 this.mTextHelper = new AppCompatTextHelper(this);

    AppCompatTextHelper(TextView view) { 
   
        this.mView = view;
        this.mAutoSizeTextHelper = new AppCompatTextViewAutoSizeHelper(this.mView);
    }

    AppCompatTextViewAutoSizeHelper(TextView textView) { 
   
        this.mTextView = textView;
        this.mContext = this.mTextView.getContext();
    }

可知,this.mTextHelper = new AppCompatTextHelper(this);旨在初始化,还未涉及autosize属性的初始化
现在来看this.mTextHelper.loadFromAttributes(attrs, defStyleAttr); 从名称看,该方法用于加载属性

//AppCompatTextHelper.java
    @SuppressLint({ 
   "NewApi"})
    void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { 
   
        ......
        this.mAutoSizeTextHelper.loadFromAttributes(attrs, defStyleAttr);
        if (AutoSizeableTextView.PLATFORM_SUPPORTS_AUTOSIZE && this.mAutoSizeTextHelper.getAutoSizeTextType() != 0) { 
   
            int[] autoSizeTextSizesInPx = this.mAutoSizeTextHelper.getAutoSizeTextAvailableSizes();
            if (autoSizeTextSizesInPx.length > 0) { 
   
                if ((float)this.mView.getAutoSizeStepGranularity() != -1.0F) { 
   
                    this.mView.setAutoSizeTextTypeUniformWithConfiguration(this.mAutoSizeTextHelper.getAutoSizeMinTextSize(), this.mAutoSizeTextHelper.getAutoSizeMaxTextSize(), this.mAutoSizeTextHelper.getAutoSizeStepGranularity(), 0);
                } else { 
   
                    this.mView.setAutoSizeTextTypeUniformWithPresetSizes(autoSizeTextSizesInPx, 0);
                }
            }
        }
        ......
    }

可以看到,该方法中存在setAutoSizeTextXXX 然而它要求AutoSizeableTextView.PLATFORM_SUPPORTS_AUTOSIZE为true,也就是VERSION.SDK_INT >= 27; 这里可以解释在API28上app设置autosize属性生效

继续看this.mAutoSizeTextHelper.loadFromAttributes(attrs, defStyleAttr);

//AppCompatTextViewAutoSizeHelper.java
    void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { 
   
        ......
        if (this.supportsAutoSizeText()) { 
   
            if (this.mAutoSizeTextType == 1) { 
   
                ......
                this.setupAutoSizeText();
            }
        } else { 
   
            this.mAutoSizeTextType = 0;
        }
    }

//这里的mTextView是AppCompatRadioButton 应该返回true
    private boolean supportsAutoSizeText() { 
   
        return !(this.mTextView instanceof AppCompatEditText);
    }

//这个方法设置了一些属性
    private boolean setupAutoSizeText() { 
   
        if (this.supportsAutoSizeText() && this.mAutoSizeTextType == 1) { 
   
            if (!this.mHasPresetAutoSizeValues || this.mAutoSizeTextSizesInPx.length == 0) { 
   
                int autoSizeValuesLength = 1;

                for(float currentSize = (float)Math.round(this.mAutoSizeMinTextSizeInPx); Math.round(currentSize + this.mAutoSizeStepGranularityInPx) <= Math.round(this.mAutoSizeMaxTextSizeInPx); currentSize += this.mAutoSizeStepGranularityInPx) { 
   
                    ++autoSizeValuesLength;
                }

                int[] autoSizeTextSizesInPx = new int[autoSizeValuesLength];
                float sizeToAdd = this.mAutoSizeMinTextSizeInPx;

                for(int i = 0; i < autoSizeValuesLength; ++i) { 
   
                    autoSizeTextSizesInPx[i] = Math.round(sizeToAdd);
                    sizeToAdd += this.mAutoSizeStepGranularityInPx;
                }

                this.mAutoSizeTextSizesInPx = this.cleanupAutoSizePresetSizes(autoSizeTextSizesInPx);
            }

            this.mNeedsAutoSizeText = true;
        } else { 
   
            this.mNeedsAutoSizeText = false;
        }

        return this.mNeedsAutoSizeText;
    }

到这里后续已经没有调用了,也就是说,在API27以下AppCompatRadioButton并没有实现autosize属性

那么AppCompatTextView和AppCompatButton是如何实现的呢?
这里主要分析AppCompatTextView

public class AppCompatTextView extends TextView implements TintableBackgroundView,
        TintableCompoundDrawablesView, AutoSizeableTextView { 
   
        ......
    public AppCompatTextView(
            @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
   
        super(TintContextWrapper.wrap(context), attrs, defStyleAttr);

        ThemeUtils.checkAppCompatTheme(this, getContext());

        mBackgroundTintHelper = new AppCompatBackgroundHelper(this);
        mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr);

        mTextHelper = new AppCompatTextHelper(this);
        mTextHelper.loadFromAttributes(attrs, defStyleAttr);
        mTextHelper.applyCompoundDrawablesTints(); //比RadioButton多出

        mTextClassifierHelper = new AppCompatTextClassifierHelper(this);
    }
    ......
    public void setAutoSizeTextTypeWithDefaults(
            @TextViewCompat.AutoSizeTextType int autoSizeTextType) { 
   
        if (Build.VERSION.SDK_INT >= 26) { 
   
            super.setAutoSizeTextTypeWithDefaults(autoSizeTextType);
        } else { 
   
            if (mTextHelper != null) { 
   
                mTextHelper.setAutoSizeTextTypeWithDefaults(autoSizeTextType);
            }
        }
    }
    public void setAutoSizeTextTypeUniformWithConfiguration(
            int autoSizeMinTextSize,
            int autoSizeMaxTextSize,
            int autoSizeStepGranularity,
            int unit) throws IllegalArgumentException { 
   
        if (Build.VERSION.SDK_INT >= 26) { 
   
            super.setAutoSizeTextTypeUniformWithConfiguration(
                    autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, unit);
        } else { 
   
            if (mTextHelper != null) { 
   
                mTextHelper.setAutoSizeTextTypeUniformWithConfiguration(
                        autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, unit);
            }
        }
    }
    ......

相比于AppCompatRadioButton,它实现了AutoSizeableTextView接口,重写了setAutoSizeTextXXX方法,因此可以实现autosize属性

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/135086.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • win32怎么用_win32function failed

    win32怎么用_win32function failedHWNDhwndFound//=FindWindow(_T("RC352_Win32"),NULL); =GetConsoleWindow();

    2022年8月5日
    7
  • NLTK FreqDist

    NLTK FreqDistFreqDisknltkFreqDisk函数能够统计数组当中单词出现的次数。text=[‘hadoop’,’spark’,’hive’,’hadoop’,’hadoop’,’spark’,’lucene’,’hadoop’,’spark’,’hive’,’hadoop’,’hadoop’,’spark’,’pig’,’zookeeper’,’flume’,’…

    2025年6月24日
    1
  • ZBar 源码分析 beta

    ZBar 源码分析 beta前言身在物流行业 扫描条码的场景很多 为了改造 ZBar 使其返回条码方向 条码与水平线的夹角 阅读了 ZBar 的源码 总结一下 没有弄清楚所有细节 只说说我弄清楚的部分 主流程应该大家都知道 ZBar 是 Z 字型扫描的 为什么要 Z 字型扫描 不清楚 一次扫描一行或者一列 在扫描过程中做滤波 EWMA 指数加权移动平均 可以消除部分噪声影响 然后计算梯度变化 确定边缘 计算当前

    2025年11月28日
    4
  • git添加用户名和邮箱「建议收藏」

    git添加用户名和邮箱「建议收藏」想了解更多数据结构以及算法题,可以关注微信公众号“数据结构和算法”,每天一题为你精彩解答。也可以扫描下面的二维码关注

    2025年8月28日
    4
  • mysql 动态新建以及删除分区表

    因为项目需要,最近研究了一下在mysql数据库下如何动态新建以及删除分区表。如果全部借助存储过程的话,新建以及删除分区表在逻辑上比较死板、不灵活,而且还容易出错。因此,我新建了一个数据表table_f

    2021年12月21日
    49
  • 手撸 webpack4.x 配置(二)[通俗易懂]

    手撸 webpack4.x 配置(二)[通俗易懂]接着上一篇手撸webpack4.x配置(一)继续学习webpack配置。今天我学习配置下webpack中另一个模块plugins配置。之前我们都是手动在打包后(dist)目录里手动新建的index.html然后把打包后生成的JS文件手动的引入,今天我们来安装一个插件让webpack自动给我们生成模板!1官网配置地址:html-webpack-p…

    2022年8月22日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号