
Android
使用 TypeArray 是 Android 开发中的一个重要工具,它可以帮助我们在 XML 文件中存储自定义对象,并在代码中检索它们。这在我们需要在多个地方使用相同的自定义对象时非常有用。本文将介绍如何在 XML 中存储自定义对象,并使用 TypeArray 检索它们的方法。
在 Android 开发中,我们经常需要在 XML 文件中定义一些属性,以便在代码中使用。例如,我们可能需要定义一个自定义的图标,然后在不同的布局文件中使用它。此时,我们可以使用 TypeArray 来实现这个目标。首先,我们需要在 res/values 文件夹下创建一个名为 attrs.XML 的文件。在这个文件中,我们可以定义我们自己的属性。下面是一个示例:XML<resources> <declare-styleable name="CustomView"> <attr name="customIcon" format="reference" /> <attr name="customText" format="string" /> </declare-styleable></resources>在这个示例中,我们定义了一个名为 CustomView 的自定义属性。它包含了两个子属性,一个是 customIcon,格式为 reference,表示它是一个图标的引用;另一个是 customText,格式为 string,表示它是一个字符串。接下来,我们可以在我们的布局文件中使用这些自定义属性。例如,我们可以创建一个名为 custom_view.XML 的布局文件,并在其中使用 CustomView 的自定义属性。示例如下:
XML<LinearLayout XMLns:Android="Android.com/apk/res/Android">http://schemas.Android.com/apk/res/Android</a>" XMLns:custom="http://schemas.Android.com/apk/res-auto" Android:layout_width="match_parent" Android:layout_height="match_parent" Android:orientation="vertical"> <com.example.CustomView</p> Android:layout_width="wrap_content" Android:layout_height="wrap_content" custom:customIcon="@drawable/ic_custom_icon" custom:customText="Hello CustomView" /></LinearLayout>在这个布局文件中,我们使用了 XMLns:custom="http://schemas.Android.com/apk/res-auto" 来引用我们定义的自定义属性。然后,在 CustomView 中使用了 custom:customIcon 和 custom:customText 属性来设置自定义图标和文本。现在,我们需要在代码中检索这些自定义属性。我们可以使用 TypedArray 来获取这些属性的值。下面是一个示例代码:
JavaTypedArray typedArray = context.obtAInStyledAttributes(attrs, R.styleable.CustomView);Drawable customIcon = typedArray.getDrawable(R.styleable.CustomView_customIcon);String customText = typedArray.getString(R.styleable.CustomView_customText);typedArray.recycle();在这个示例代码中,我们首先使用 context.obtAInStyledAttributes(attrs, R.styleable.CustomView) 方法获取 TypedArray 对象。其中,attrs 是在构造函数中传递的 AttributeSet 对象,R.styleable.CustomView 是我们在 attrs.XML 中定义的自定义属性集合。然后,我们可以使用 typedArray.getDrawable(R.styleable.CustomView_customIcon) 来获取自定义图标的引用,使用 typedArray.getString(R.styleable.CustomView_customText) 来获取自定义文本的值。最后,我们需要记得调用 typedArray.recycle() 方法来回收 TypedArray 对象。使用 TypeArray 存储自定义对象的示例代码:
XML<!-- attrs.XML --><resources> <declare-styleable name="CustomView"> <attr name="customIcon" format="reference" /> <attr name="customText" format="string" /> </declare-styleable></resources>
XML<!-- custom_view.XML --><LinearLayout XMLns:Android="Android.com/apk/res/Android">http://schemas.Android.com/apk/res/Android</a>" XMLns:custom="http://schemas.Android.com/apk/res-auto" Android:layout_width="match_parent" Android:layout_height="match_parent" Android:orientation="vertical"> <com.example.CustomView</p> Android:layout_width="wrap_content" Android:layout_height="wrap_content" custom:customIcon="@drawable/ic_custom_icon" custom:customText="Hello CustomView" /></LinearLayout>
Java// CustomView.Javapublic class CustomView extends View { private Drawable customIcon; private String customText; public CustomView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtAInStyledAttributes(attrs, R.styleable.CustomView); customIcon = typedArray.getDrawable(R.styleable.CustomView_customIcon); customText = typedArray.getString(R.styleable.CustomView_customText); typedArray.recycle(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制自定义图标和文本 }}在这个示例中,我们定义了一个 CustomView 类,继承自 View,并在构造函数中使用 TypedArray 获取自定义属性的值。然后,在 onDraw 方法中我们可以使用这些属性值来绘制自定义图标和文本。使用 TypeArray 可以帮助我们在 XML 文件中存储自定义对象,并在代码中检索它们。这样可以提高代码的复用性和可读性,使得我们可以更方便地在不同的地方使用相同的自定义对象。希望本文对你理解 Android 中的 TypeArray 有所帮助。Copyright © 2025 IZhiDa.com All Rights Reserved.
知答 版权所有 粤ICP备2023042255号