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

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

问题描述

我正在阅读 Beginning Android 4 Development,在第 5 章中讨论了 GalleryImageVievs 并介绍了 declare-styleable 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/values/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; 
        }
    }
}

我已经阅读了几次代码,但我真的不明白用一个 attr 子元素定义这个具有样式的 Gallery1 的目的,而只有一个 >name 属性.. 你能帮我吗?这个 galleryItemBackground 是系统提供的还是我们定义的?我们在这段代码中做了什么?

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?

在此先感谢您的帮助!

推荐答案

此标签是 R.Styleable,它可以与来自 android: xml 命名空间前缀的自定义样式标签区分开来在属性名称之前.

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.

此特定属性描述为:

画廊项目的首选背景.这应该设置为您从适配器提供的任何视图的背景.

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.

是否有任何官方文档页面对此进行了深入解释样式?

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 使用 <declare-styleable> 标签创建一个 AttributeSet.TypedArray 用于解析AttributeSet.

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

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

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 中设置某些属性而不必在代码中完成所有操作,将 UI 的外观"与其功能"分开.以 View 类为例.它有超过 30 个可以在 xml 文件中设置的属性(大小、填充、可点击、可聚焦等);制作 View 的自定义子类的人可以在 xml 中设置一些、全部或不设置这些属性,并且在创建视图时会自动为您处理这些属性.如果需要,可以使用等效的代码来设置属性,但是想象一下,每次对 View 进行子类化时,您都必须在代码中设置所有属性,而不是在 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天全站免登陆