自定义View
1.自定义属性
在values目录下创建一个attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MNView">
<attr name="mn_color" format="string"></attr>
<attr name="mn_size" format="dimension"></attr>
<attr name="mn_text" format="string"></attr>
</declare-styleable>
</resources>
2.在布局文件中使用自定义的view和viewGroup
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.customview.MNViewGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#EFC2D1"
android:padding="20dp"
tools:ignore="MissingConstraints">
<com.example.customview.MNView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_marginLeft="20dp"
app:mn_text="迷你"
android:background="@color/teal_200" />
<com.example.customview.MNView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding