com.android.support:support 升级到 28.0.0 所遇问题

    xiaoxiao2022-07-13  193

    com.android.support:support-v4:25.4.0 升级为: com.android.support:support-v4:28.0.0

    1.统一修改为 28.0.0 后编译报错

    编译时各种稀奇古怪的错,如org.gradle.tooling.BuildException: Failed to process resources, see aapt output above for details. 修改 compileSdkVersion = 28即可

    2.设置TabLayout点击无水波纹效果,失效

    之前使用的属性为: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" />

    3.通过反射修改 TabLayout 下划线宽度,失效

    因新的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); } }

    4.通过反射为单独TabLayout.Tab设置点击事件,失效

    private void setTabClick(TabLayout tabLayout) { for (int i = 0; i < tabLayout.getTabCount(); i++) { TabLayout.Tab tab = tabLayout.getTabAt(i); if (tab == null) { return; } //这里使用到反射,拿到Tab对象后获取Class Class c = tab.getClass(); try { //Filed “字段、属性”的意思,c.getDeclaredField 获取私有属性。 //"mView"是Tab的私有属性名称(可查看TabLayout源码),类型是 TabView,TabLayout私有内部类。 // 25.4.0 // Field field = c.getDeclaredField("mView"); // 28.0.0 Field field = c.getDeclaredField("view"); //值为 true 则指示反射的对象在使用时应该取消 Java 语言访问检查。值为 false 则指示反射的对象应该实施 Java 语言访问检查。 field.setAccessible(true); final View view = (View) field.get(tab); if (view == null) { return; } view.setTag(i); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //这里就可以根据业务需求处理点击事件了。 } }); } catch (Exception e) { e.printStackTrace(); } } }

    5.自己为了修改TabLayout的宽度的 MyTabLayout ,下划线没有了

    替换为最新的 TabLayout,增加属性即可

    app:tabIndicator="@drawable/shape_tab_indicator"

    注意:Android 6.0 以下版本此属性不生效,可以直接使用图片试试

    6.通过CollapsingToolbarLayout实现的滑动置顶

    上滑时,之前滑动一小段松手会快速置顶,速度快时会感觉到卡顿现象。最新的不会,滑动一点就是一点(可能不好表达?)。下滑时,之前惯性滑动后,置顶布局会定在那里,需要再次滑动才会下来。最新的直接会下来。

    更新后显然更好用了,?

    7.通过反射得到CollapsingToolbarLayout中标题的画笔,失效

    之前反射得到的字段名更改

    /** * 通过反射得到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; } }

    8.其他

    引入的三方库可能不兼容 28.0.0设置TabLayout的字体大小,对应的属性为app:tabTextAppearance使用style时,遇到app:tabIndicator="@drawable/shape_tab_indicator"属性,可这样使用 <style name="tabLayoutStyle"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">44dp</item> <item name="tabIndicator">@drawable/shape_tab_indicator</item> <item name="tabSelectedTextColor">@color/color_black_title</item> <item name="tabTextAppearance">@style/MyTabTextAppearance</item> </style> <style name="MyTabTextAppearance" parent="TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"> <item name="android:textSize">12sp</item> </style>
    最新回复(0)