美文网首页
用BindingAdapter在layout中写xml背景

用BindingAdapter在layout中写xml背景

作者: 进击的小大叔 | 来源:发表于2020-02-12 17:29 被阅读0次

    创建DefaultBindingAdapter

    当下几乎所有的开发都基于mvvm 即databinding

    GradientDrawable

    
        /**
         * shape {@link GradientDrawable#RECTANGLE,GradientDrawable#OVAL,GradientDrawable#LINE,GradientDrawable#RING}
         * 分别为 0,1,2,3
         *
         * @param view
         * @param solid
         */
        @BindingAdapter(value = {"shape", "solid", "strokeWidth", "strokeColor", "radius", "radiusLeftTop", "radiusLeftBottom", "radiusRightTop", "radiusRightBottom"},requireAll = false)
        public static void gradientDrawable(View view, int shape, int solid, int strokeWidth, int strokeColor, int radius, int radiusLeftTop, int radiusLeftBottom, int radiusRightTop, int radiusRightBottom) {
            float[] floats;
            if (radius > 0) {
                floats = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
            } else {
                floats = new float[]{radiusLeftTop, radiusLeftTop, radiusRightTop, radiusRightTop, radiusRightBottom, radiusRightBottom, radiusLeftBottom, radiusLeftBottom};
            }
            view.setBackground(GradientDrawableFactory.createDrawable(shape, solid, strokeWidth, strokeColor, floats));
        }
    
    

    使用

    <layout 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">
    
        <data>
    
        </data>
            <RelativeLayout
                android:id="@+id/rl_home_top"
                android:layout_width="match_parent"
                shape="@{1}"
                solid="@{@color/color_333333}"
                strokeColor="1"
                strokeWidth="@{@color/color_333333}"
                radius="@{3}"
                radiusLeftBottom="@{3}"
                radiusLeftTop="@{3}"
                radiusRightBottom="@{3}"
                radiusRightTop="@{3}"
                android:layout_height="70dp">
             </RelativeLayout>
    
    </Layout>
    
    

    StateListDrawable

    /**
         *  需要press 状态的背景
         * @param view
         * @param unPressDrawable
         * @param pressDrawable
         */
        @BindingAdapter(value = {"unPressDrawableId","pressDrawableId"})
        public static void pressDrawable(View view,int unPressDrawable,int pressDrawable){
            view.setBackground(StateListDrawableFactory.createPressDrawable(unPressDrawable,pressDrawable));
        }
        /**
         *  需要press 状态的背景
         * @param view
         * @param unPressDrawable
         * @param pressDrawable
         */
        @BindingAdapter(value = {"unPressDrawable","pressDrawable"})
        public static void pressDrawable(View view, Drawable unPressDrawable, Drawable pressDrawable){
            view.setBackground(StateListDrawableFactory.createPressDrawable(unPressDrawable,pressDrawable));
        }
    
        /**
         *  类似checkbox 场景
         * @param view
         * @param unCheckDrawable
         * @param checkDrawable
         */
        @BindingAdapter(value = {"unCheckDrawableId","checkDrawableId"})
        public static void checkDrawable(View view,int unCheckDrawable,int checkDrawable){
            view.setBackground(StateListDrawableFactory.createPressDrawable(unCheckDrawable,checkDrawable));
        }
    
        /**
         *  类似checkbox 场景
         * @param view
         * @param unCheckDrawable
         * @param checkDrawable
         */
        @BindingAdapter(value = {"unCheckDrawable","checkDrawable"})
        public static void checkDrawable(View view,Drawable unCheckDrawable,Drawable checkDrawable){
            view.setBackground(StateListDrawableFactory.createPressDrawable(unCheckDrawable,checkDrawable));
        }
    
        /**
         * 背景选中状态
         * @param view
         * @param unSelectDrawable
         * @param selectDrawable
         */
        @BindingAdapter(value = {"unSelectDrawableId","selectDrawableId"})
        public static void selectDrawable(View view,int unSelectDrawable,int selectDrawable){
            view.setBackground(StateListDrawableFactory.createSelectDrawable(unSelectDrawable,selectDrawable));
        }
        /**
         * 背景选中状态
         * @param view
         * @param unSelectDrawable
         * @param selectDrawable
         */
        @BindingAdapter(value = {"unSelectDrawable","selectDrawable"})
        public static void selectDrawable(View view,Drawable unSelectDrawable,Drawable selectDrawable){
            view.setBackground(StateListDrawableFactory.createSelectDrawable(unSelectDrawable,selectDrawable));
        }
    
    

    使用

                   <Button
                   checkDrawableId="@{@drawable/check}"
                    unCheckDrawableId="@{@drawable/unCheck}"
                    android:layout_marginLeft="27dp"
                    android:layout_marginRight="27dp"/>
    
    
    press 及 select 依上

    也支持drawable直接

     <Button
                   checkDrawable="@{GradientDrawableFactory.createDrawable(shape, solid, strokeWidth, strokeColor, floats)}"
                    unCheckDrawable="@{GradientDrawableFactory.createDrawable(shape, solid, strokeWidth, strokeColor, floats)}"
                    android:layout_marginLeft="27dp"
                    android:layout_marginRight="27dp"/>
    
    

    TextView 画线

    下划线

        underLine="@{true}"
    
    

    删除线

      deleteLine="@{true}"
    
    

    网络图片加载

        imageUrl='@{"https://wwgsdfs"}'
        placeDrawableId="@{@drawable/...}"
        errorDrawableId="@{@drawable/...}"
    
    

    注意在databinding 在"@{}"中写字符串时需要将外层的""改成''

    相关文章

      网友评论

          本文标题:用BindingAdapter在layout中写xml背景

          本文链接:https://www.haomeiwen.com/subject/cbucfhtx.html