Android 开发 Tip 11 -- TabLayout 设置文字大小一致
来源:互联网 发布:mac系统支持steam吗 编辑:程序博客网 时间:2024/06/08 09:36
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/71425135
使用TabLayout的时候,有些tab的文字比较多时,会出现与其他tab的文字大小不一致的情况!
TabLayout 继承自 HorizontalScrollView
内部有几个关键的类:
SlidingTabStrip – TabLayout的唯一子View
Tab – 子item的实体类,包含text, icon, customView等属性
TabView – 每个Tab的布局
ViewPagerOnTabSelectedListener – – 监听滑动
TabLayoutOnPageChangeListener – 监听滑动
关于每个tab的文字大小的设置关键代码就在 TabView.onMeasure() 之中!
@Override public void onMeasure(final int origWidthMeasureSpec, final int origHeightMeasureSpec) { // ... // We need to switch the text size based on whether the text is spanning 2 lines or not if (mTextView != null) { final Resources res = getResources(); float textSize = mTabTextSize; int maxLines = mDefaultMaxLines; if (mIconView != null && mIconView.getVisibility() == VISIBLE) { // If the icon view is being displayed, we limit the text to 1 line maxLines = 1; } else if (mTextView != null && mTextView.getLineCount() > 1) { // Otherwise when we have text which wraps we reduce the text size textSize = mTabTextMultiLineSize; } final float curTextSize = mTextView.getTextSize(); final int curLineCount = mTextView.getLineCount(); final int curMaxLines = TextViewCompat.getMaxLines(mTextView); if (textSize != curTextSize || (curMaxLines >= 0 && maxLines != curMaxLines)) { // We've got a new text size and/or max lines... boolean updateTextView = true; if (mMode == MODE_FIXED && textSize > curTextSize && curLineCount == 1) { // If we're in fixed mode, going up in text size and currently have 1 line // then it's very easy to get into an infinite recursion. // To combat that we check to see if the change in text size // will cause a line count change. If so, abort the size change and stick // to the smaller size. final Layout layout = mTextView.getLayout(); if (layout == null || approximateLineWidth(layout, 0, textSize) > getMeasuredWidth() - getPaddingLeft() - getPaddingRight()) { updateTextView = false; } } if (updateTextView) { mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); mTextView.setMaxLines(maxLines); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } } }
TabLayout内部设置了两种字体大小:
float mTabTextSize;float mTabTextMultiLineSize;
final TypedArray ta = context.obtainStyledAttributes(mTabTextAppearance, android.support.v7.appcompat.R.styleable.TextAppearance); try { mTabTextSize = ta.getDimensionPixelSize( android.support.v7.appcompat.R.styleable.TextAppearance_android_textSize, 0); mTabTextColors = ta.getColorStateList( android.support.v7.appcompat.R.styleable.TextAppearance_android_textColor); } finally { ta.recycle(); }
<style name="TextAppearance.Design.Tab" parent="TextAppearance.AppCompat.Button"> <item name="android:textSize">@dimen/design_tab_text_size</item> <item name="android:textColor">?android:textColorSecondary</item> <item name="textAllCaps">true</item></style>
<dimen name="design_tab_text_size">14sp</dimen>
默认字体大小是14sp
final Resources res = getResources();mTabTextMultiLineSize = res.getDimensionPixelSize(R.dimen.design_tab_text_size_2line);
<dimen name="design_tab_text_size_2line">12sp</dimen>
多行时字体大小为12sp
结合onMeasure()来分析!
若使字体大小保持一致,一种方式是修改 mTabTextSize 这个默认字体大小;另外一种方式就是修改 mTabTextMultiLineSize 这个字体大小!
方式一:
定义一个style,把默认字体大小设置成12sp!
<style name="TextAppearance.Design.Tab.Custom" parent="TextAppearance.AppCompat.Button"> <item name="android:textSize">12sp</item> <item name="android:textColor">?android:textColorSecondary</item> <item name="textAllCaps">true</item> </style>
在TabLaout中引用
<android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabTextAppearance="@style/TextAppearance.Design.Tab.Custom" />
方式二:
把多行字体大小设置位于默认字体大小一致!
<dimen name="design_tab_text_size" tools:ignore="PrivateResource">14sp</dimen>
1 0
- Android 开发 Tip 11 -- TabLayout 设置文字大小一致
- Android 自定义设置文字大小
- Android开发tip
- Android中设置文字大小的定义类型
- Android开发之设置TabLayout下方下划线的宽度
- Android开发 设置tablayout下划线长度和内容保持一致
- Android EditText 中hint文字大小以及与输入文字颜色保存一致
- Android TabLayout 设置Tab 字体大小
- Android 开发 Tip 9 -- TouchDelegate
- Android 开发 Tip 13 -- generateViewId
- Android 开发 Tip 14 -- Singleton
- android设置系统文字大小后App的文字会变化
- Android-Toolbar title文字大小设置及居中显示
- Android中动态设置布局高度一致
- Latex文字大小设置
- Qt设置全局文字大小
- Eclipse下设置文字大小
- TextView设置文字大小
- 拉格朗日插补法总结
- java内部类
- serializable 接口的作用
- C++模板:函数模板、类模板、模板与继承
- 【案例】简单判断— 分支结构的应用
- Android 开发 Tip 11 -- TabLayout 设置文字大小一致
- 开发遇到的问题
- IE兼容模式问题
- 海康威视(Hikvision)基于设备SDK中链接错误error LNK2019和error LNK2001
- 关于MySQL Java Driver 的useOldAliasMetadataBehavior参数
- Linux设备模型(4)_sysfs
- ngDialog的套用
- 初探vue-router
- NSMutableAttributedString 结合 UILabel显示 Html