当前位置: 代码迷 >> 综合 >> Android自定义View、ViewGroup
  详细解决方案

Android自定义View、ViewGroup

热度:11   发布时间:2023-12-05 11:24:03.0

自定义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.MNViewGroupandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#EFC2D1"android:padding="20dp"tools:ignore="MissingConstraints"><com.example.customview.MNViewandroid: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.MNViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:padding
  相关解决方案