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

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

问题描述

我有一个包含两个TextViews有一个自定义大小,设置方法的自定义组件(两个文本视图比例约1:2的比例)。作为,这是RelativeLayout的子类,它没有一个TEXTSIZE属性,但我不知道是否有可能仍然设置了安卓TEXTSIZE 属性的XML实例化这个组件,然后抓住从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格式)已TEXTSIZE定义14sp:

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.

您必须声明的属性名称(S)在attrs.xml文件(值目录下),以便在任何你去使用你的自定义视图,从这些属性收集到的值将在构造函数递。

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>

现在你可以设置你的TextViews'大小在你的XML,但并非没有宣布在布局的命名空间,方法如下:

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"     
    />

请注意我是如何声明的名称空间是乔希在您的CustomView的属性所设置的第一道防线。

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

我希望这有助于乔希,

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

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