访问自定义组件的 AttributeSet 中的属性 [英] Accessing attrs in AttributeSet for custom components

查看:30
本文介绍了访问自定义组件的 AttributeSet 中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义组件,其中包含两个具有自定义大小设置方法的 TextView(两个文本视图的比例约为 1:2).由于这是 RelativeLayout 的子类,它没有 textSize 属性,但我想知道是否仍然可以在此组件的 XML 实例化中设置 android:textSize 属性,然后从 AttributeSet 中获取 textSize 属性,以便在构造函数中与我的自定义 setSize() 方法一起使用.

I've got a custom component containing two TextViews that have a custom size-setting method on (the two text views are proportional by about a 1:2 ratio). Being that this is a subclass of RelativeLayout, it doesn't have a textSize attribute, but I'm wondering if it is possible to still set the android:textSize attribute in the XML instantiation for this component, and then grab the textSize attribute from the AttributeSet to use with my custom setSize() method in the constructor.

我已经看到了使用自定义属性执行此操作的技术,但是如果我想获取一个已经在 android 词典中的属性怎么办?

I've seen the technique to do this with custom attributes, but what if I want to grab an attribute that's already in the android lexicon?

推荐答案

是的,有可能;

让我们假设您的 RelativeLayout 声明(在 xml 中)具有使用 14sp 定义的 textSize:

let's assume your RelativeLayout declaration (in xml) has textSize defined with 14sp:

android:textSize="14sp"

在自定义视图的构造函数中(接受 AttributeSet 的构造函数),您可以像这样从 Android 的命名空间中检索属性:

In the constructor of your custom view (the one that takes in the AttributeSet), you can retrieve the attributes from Android's namespace as such:

String xmlProvidedSize = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");

xmlProvidedSize 的值将类似于14.0sp",也许只需稍加编辑字符串,您就可以提取数字.

The value of the xmlProvidedSize will be something like this "14.0sp" and maybe with little bit of String editing you can just extract the numbers.

另一种声明您自己的属性集的选项会有点冗长,但也是可能的.

Another option to declare your own attribute set would be little lengthy, but it is also possible.

所以,你有你的自定义视图和你的 TextViews 声明如下:

So, you have your custom view and your TextViews declared something like this right:

public class MyCustomView extends RelativeLayout{

    private TextView myTextView1;
    private TextView myTextView2;

// rest of your class here

太好了...

现在您还需要确保您的自定义视图覆盖了接受 AttributeSet 的构造函数,如下所示:

Now you need to also make sure your custom view overrides the constructor that takes in the AttributeSet like this:

public MyCustomView(Context context, AttributeSet attrs){   
    super(context, attrs);
    init(attrs, context);  //nice, clean method to instantiate your TextViews// 
}

好的,现在让我们看看 init() 方法:

ok, let's see that init() method now:

private void init(AttributeSet attrs, Context context){
    // do your other View related stuff here //


    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyCustomView);
    int xmlProvidedText1Size = a.int(R.styleable.MyCustomView_text1Size);
    int xmlProvidedText2Size = a.int(R.styleable.MyCustomView_text2Size);

    myTextView1.setTextSize(xmlProvidedText1Size);
    myTextView2.setTextSize(xmlProvidedText2Size);

    // and other stuff here //
}

您可能想知道 R.styleable.MyCustomView、R.styleable.MyCustomView_text1Size 和 R.styleable.MyCustomView_text2Size 是从哪里来的;请允许我详细说明这些.

You're probably wondering where does R.styleable.MyCustomView, R.styleable.MyCustomView_text1Size and R.styleable.MyCustomView_text2Size are coming from; allow me to elaborate on those.

您必须在 attrs.xml 文件(在 values 目录下)声明属性名称,这样无论您在哪里使用自定义视图,从这些属性收集的值都将传递到您的构造函数中.

You have to declare the attribute name(s) in your attrs.xml file (under values directory) so that where ever you get to use your custom view, the values gathered from these attributes will be handed in your constructor.

那么让我们看看您如何按照您的要求声明这些自定义属性:这是我的整个 attrs.xml

So let's see how you declare the these custom attributes like you've asked: Here is my whole attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyCustomView">
        <attr name="text1Size" format="integer"/>
        <attr name="text2Size" format="integer"/>
    </declare-styleable>
</resources>

现在您可以在 XML 中设置 TextViews 的大小,但不能在布局中声明命名空间,方法如下:

Now you can set your TextViews' size in your XML, but NOT without declaring the namespace in your Layout, here is how:

<com.my.app.package.MyCustomView
    xmlns:josh="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_custom_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    josh:text1Size="15"
    josh:text2Size="30"     
    />

请注意我如何将命名空间声明为josh"作为您的 CustomView 属性集中的第一行.

Please pay attention how I declared the namespace to be "josh" as the first line in your CustomView's attribute set.

我希望这对乔希有所帮助,

I hope this helps Josh,

这篇关于访问自定义组件的 AttributeSet 中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆