view

自定义viwe的小细坑

2019-10-09  本文已影响0人  奈蜇

这里说的是一种最简单的自定义view的方式,网上教程最多的一种。

//继承某某布局,然后加载自定义的布局
 LayoutInflater inflater = LayoutInflater.from(context);
 View view = inflater.inflate(R.layout.XXXX, this);

网上的教程千篇一律,都是这样教的,但我缺发现了一点点小坑,就是这个方法是存在布局冗余的。
为什么这样说? 给你们看例子...

栗子:
目标自定义view的效果:



一般网络上的实现方法:
Class:

public class VipCardView extends FrameLayout {

    private TextView textView1;
    private TextView textView2;
    private TextView textView3;


    public VipCardView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public VipCardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VipCardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(@NonNull Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.layout_vip_card, this);
        if (view != null) {
            textView1 = view.findViewById(R.id.lvc_tv1);
            textView2 = view.findViewById(R.id.lvc_tv2);
            textView3 = view.findViewById(R.id.lvc_tv3);
        }
    }

    public void setData(@NonNull String s1, @NonNull String s2, @NonNull String s3) {
        textView1.setText(s1);
        textView2.setText(s2);
        textView3.setText(s3);
    }

}

加载的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:layout_margin="30pt">

    <TextView
        android:id="@+id/lvc_tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25pt"
        android:layout_marginTop="35pt"
        android:textColor="@color/white"
        android:textSize="35pt"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/lvc_tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="46pt"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/lvc_tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="12pt"
        android:layout_marginBottom="35pt"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

使用view:


这里设置了一个圆角背景

我们看看实现的效果图:


效果图
emmm...是不是发现什么了哦
为什么有两个背景??? 仔细看看我的xml

继续使用layout inspector (as 菜单 -> tools) 看看布局结构



??? 为什么多了一级嵌套 这不就冗余了啊
解答:
使用这种方式自定义view 原理其实就是把布局文件当作一个view加载到原布局中去。这里必然会new 一个你view继承的布局,这就产生了冗余。

解决问题:

在自定义xml中使用merge作为根节点
如:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/lvc_tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="25pt"
        android:layout_marginTop="35pt"
        android:textColor="@color/white"
        android:textSize="35pt"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/lvc_tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="46pt"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/lvc_tv3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="12pt"
        android:layout_marginBottom="35pt"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</merge>

并继承之前xml的根布局

public class VipCardView extends ConstraintLayout {

看看效果:



嵌套消失了 ok 完美

原理:
merge 标签可以用来代替布局文件中的根节点,例如LinearLayout,RelativeLayout,FrameLayout等元素。配合include标签,可以减少布局的嵌套。

虽然并没有什么用,但能优化布局一点点。。。
emmm好像真的没什么用

参考:https://blog.csdn.net/demonliuhui/article/details/74505233

上一篇下一篇

猜你喜欢

热点阅读