有人可以解释我的声明,设置样式在这个例子中的XML标记及其使用背后的理论? [英] Can someone explain me declare-styleable XML tag in this example and the theory behind its use?

查看:145
本文介绍了有人可以解释我的声明,设置样式在这个例子中的XML标记及其使用背后的理论?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读起的Andr​​oid 4.0开发,并在第5章它谈论的图库的和的 ImageVievs 的,并介绍了的申报,设置样式的XML标记没有解释它的宗旨。我试图找到一些信息也在参考,没有运气。例如,我们有以下几点:

I'm reading Beginning Android 4 Development and in chapter 5 it talks about Gallery and ImageVievs and introduces the declare-styleable XML tag without explaining its purpose.. I tried to find some info also on the reference, without luck.. For example we have the following:

RES /价值/ attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources>
    <declare-styleable name="Gallery1">
        <attr name="android:galleryItemBackground" />
    </declare-styleable>
</resources>

example.java

public class GalleryActivity extends Activity {
[...]
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this)); 
        [...]
    }

    [...]

    public class ImageAdapter extends BaseAdapter {
        [...]
        int itemBackground;

        public ImageAdapter(Context c) {
            context = c;
            //---setting the style---
            TypedArray a = obtainStyledAttributes(
            R.styleable.Gallery1); 
            itemBackground = a.getResourceId(
                        R.styleable.Gallery1_android_galleryItemBackground, 0);
            a.recycle();
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            [...]
            imageView.setBackgroundResource(itemBackground);
            return imageView; 
        }
    }
}

我读过codeA几次,我真的不明白一个单一的 ATTR 的孩子只能用定义这个设置样式的 Gallery1 的目的的名称的属性..你可以帮助我吗?这是的 galleryItemBackground 的一些系统提供的或者是由我们定义的东西吗?什么是我们在这一块code的干什么呢?

I've read the code a few times and I don't really understand the purpose of defining this styleable Gallery1 with a single attr child only with a name attribute.. can you help me? Is this galleryItemBackground something provided by the system or is it something defined by us? What are we doing in this piece of code?

感谢您预先的任何帮助!

Thank you in advance for any help!

推荐答案

这标签是一组的 R.Styleable ,可除了自定义设置样式标签从的android: 属性名称前的XML命名空间preFIX。

This tag is part of a set of premade Android attributes defined in R.Styleable, which can be told apart from a custom styleable tag from the android: xml namespace prefix before the attribute name.

这特殊的属性被描述为:

This particular attribute is described as:

有关库项目的preferred背景。这应被设置为
  任何意见的背景下,你从适配器提供的。

The preferred background for gallery items. This should be set as the background of any Views you provide from the Adapter.

您是对的,但是,自定义属性标签将需要的属性,不仅名字,但它的类型是什么,例如添加自定义元素的 attrs.xml 文件可能是这样的:

You are right, however, that custom attribute tags would require not only the name of the attribute but what its type is, for example adding a custom element to your attrs.xml file might look like this:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="MyCustomView"> 
        <attr name="android:galleryItemBackground" />              
        <attr name="myCustomAttr" format="string" /> 
    </declare-styleable> 
</resources>

请注意缺少的android:第二属性命名空间以及

Note the lack of the android: namespace on the second attribute as well.

编辑:

是否有深入这说明任何官方文档页面
  Styleables?

Is there any official documentation page that explains in depth this Styleables?

查看 R.attr (点击链接)包含在Android的各种属性。你并不需要声明一个类型为他们,因为他们都已经声明。 。要知道已被宣布是什么类型的特定属性,找到一个你感兴趣的说明 galleryItemBackground 是,因为你可能会想到,到另一个资源的引用;其他的可能性是布尔型,花车,颜色等。

Check out R.attr (click for link) for various attributes included in Android. You do not need to declare a type for them, because they are all already declared. To know what type has been declared for a particular attribute, find the description for the one you are interested in. galleryItemBackground is, as you may expect, a reference to another resource; other possibilities are booleans, floats, colors, etc.

其他参考:Andtoid使用&LT;申报,设置样式&GT; 标签来创建一个<一个href=\"http://developer.android.com/reference/android/util/AttributeSet.html\"><$c$c>AttributeSet. <一href=\"http://developer.android.com/reference/android/content/res/TypedArray.html\"><$c$c>TypedArray用来解析的AttributeSet

Additional references: Andtoid uses the <declare-styleable> tag to create an AttributeSet. TypedArray is used to parse the AttributeSet.

如果在code以上的目的[...]仅仅是获得一个默认
  可绘制的视图的背景,我不能设置变量
  与getDrawable(android.R.attr.galleryItemBackground)?itemBackground

If the purpose of the code above [...] is simply get a default Drawable for the view's background, couldn't I set the variable itemBackground with getDrawable(android.R.attr.galleryItemBackground)?

在这个例子中,这是很难看到这种图案的用途时,只有一个属性。你可以做你问什么,它可能会更容易。该构建,但是,Android的口头禅,让你在XML中,而不必做code一切设置某些属性从functionaly分开UI的看的一部分。就拿查看类,例如。它拥有超过30属性,可以在一个XML文件(大小,填充,可点击的,可聚焦等)进行设置;有人制作的查看自定义子类可以设置一些,全部或没有在XML这些属性,并在创建视图时,它们会自动为您处理。有code等值,如果需要设置的属性,但是想象一下,每次你的子类时间查看你必须设置code的所有属性,而不是让一个选项设置他们的XML。

In the example, it is hard to see the usefulness of this pattern when there is only one attribute. You can do what you ask, and it may be easier. The construct, however, is part of Android's mantra to separate the UI's "look" from its "functionaly" by letting you set certain attributes in xml instead of having to do everything in code. Take the View class, for example. It has over 30 attributes that can be set in an xml file (size, padding, clickable, focusable, etc); someone making a custom subclass of View can set a few, all or none of these attributes in xml and they are automatically handled for you when the view is created. There are code equivalents to set the attributes if needed, but imagine every time you subclassed View you HAD to set all attributes in code instead of having an option to set them in xml.

这也将是一件微不足道的小事,只是让自己的资源为你的类,做同样的事情,但使用内置的样式将提供,如果你不符合Android框架的外观和感觉,默认资源覆盖它们。

It would also be a trivial matter to just make your own resources for your classes that do exactly the same thing, however using the built in styles will provide default resources that match the look and feel of the Android framework if you do not override them.

希望这有助于。

这篇关于有人可以解释我的声明,设置样式在这个例子中的XML标记及其使用背后的理论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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