com.android.support:support-v4:25.4.0 升级为: com.android.support:support-v4:28.0.0
编译时各种稀奇古怪的错,如org.gradle.tooling.BuildException: Failed to process resources, see aapt output above for details. 修改 compileSdkVersion = 28即可
之前使用的属性为:app:tabBackground="@android:color/transparent" 现在需要使用属性:app:tabRippleColor="@android:color/transparent"
<android.support.design.widget.TabLayout android:id="@+id/tl_fenlei_type" android:layout_width="match_parent" android:layout_height="73dp" android:background="@color/colorWhite" android:visibility="gone" app:tabRippleColor="@android:color/transparent" app:tabIndicatorHeight="0dp" app:tabMode="scrollable" />因新的api字段名和之前的不一致或没有该字段导致。 28.0.0后,可通过api直接实现TabLayout固定宽度:
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <item android:gravity="center"> <shape> <size android:width="28dp" android:height="2dp" /> <corners android:radius="1dp" /> <solid android:color="@color/color_theme" /> </shape> </item> </layer-list>app:tabIndicator="@drawable/shape_tab_indicator"
注意:Android 6.0 以下版本此属性不生效,可以直接使用图片试试
关键源码 DrawableCompat.java:
public static Drawable wrap(@NonNull Drawable drawable) { if (VERSION.SDK_INT >= 23) { return drawable; } else if (VERSION.SDK_INT >= 21) { return (Drawable)(!(drawable instanceof TintAwareDrawable) ? new WrappedDrawableApi21(drawable) : drawable); } else { return (Drawable)(!(drawable instanceof TintAwareDrawable) ? new WrappedDrawableApi14(drawable) : drawable); } }替换为最新的 TabLayout,增加属性即可
app:tabIndicator="@drawable/shape_tab_indicator"
注意:Android 6.0 以下版本此属性不生效,可以直接使用图片试试
更新后显然更好用了,?
之前反射得到的字段名更改
/** * 通过反射得到CollapsingToolbarLayout中标题的画笔。通过它得到标题变化中的颜色 */ @RequiresApi(api = Build.VERSION_CODES.KITKAT) public static TextPaint getCollapsingTitlePaint(CollapsingToolbarLayout collapsing) { try { Class clazz = Class.forName("android.support.design.widget.CollapsingToolbarLayout"); // 25.4.0 // Field fieldTextHelper = clazz.getDeclaredField("mCollapsingTextHelper"); // 28.0.0 Field fieldTextHelper = clazz.getDeclaredField("collapsingTextHelper"); fieldTextHelper.setAccessible(true); Object obj = fieldTextHelper.get(collapsing); Class clazzHelper = Class.forName("android.support.design.widget.CollapsingTextHelper"); // 25.4.0 // Field fieldTextPaint = clazzHelper.getDeclaredField("mTextPaint"); // 28.0.0 Field fieldTextPaint = clazzHelper.getDeclaredField("textPaint"); fieldTextPaint.setAccessible(true); return (TextPaint) fieldTextPaint.get(obj); } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); return null; } }