本文介绍了Android版式-何时使用app:vs android :?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写一些Android应用程序,但我不太了解何时使用app:和何时使用android:.当样式没有按照应有的方式应用时,我会反复尝试,有时会发现使用app:而不是android:可以解决问题,但是我不明白为什么.如果有人能指出我正确的方向,那就太好了.谢谢!

I've been writing some Android apps but I don't really understand when to use app: and when to use android:. When styles are not being applied the way they're supposed to, I use trial and error and sometimes find that using app: instead of android: solves the issue but I don't understand why. It'd be great if someone could point me in the right direction. Thanks!

推荐答案

您正在谈论的是自定义命名空间.在android中,我们可以在已有视图的基础上创建自定义视图.按照Google开发人员文档中的说明.要将内置视图添加到用户界面,请在XML元素中指定它,然后使用元素属性控制其外观和行为.也可以通过XML添加编写良好的自定义视图并设置其样式.要在您的自定义视图中启用此行为,您必须:

You are talking about custom namespace.In android we can create custom views in additional to already available views.As per in Google developer docs..To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:

在资源元素中为视图定义自定义属性为您的XML布局中的属性指定值在运行时检索属性值将检索到的属性值应用于视图

Define custom attributes for your view in a resource elementSpecify values for the attributes in your XML layoutRetrieve attribute values at runtimeApply the retrieved attribute values to your view

一旦定义了自定义属性,就可以像内置属性一样在布局XML文件中使用它们.唯一的区别是您的自定义属性属于不同的名称空间.而不是属于 http://schemas.android.com/apk/res/android名称空间,它们属于 http://schemas.android.com/apk/res/[您的包名称]

Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]

因此,如果使用默认视图,则可以使用android名称空间;如果要为自定义视图设置和使用属性,则可以定义自己的名称.

So for if you use default views you can use android namespace and if you want to set and use attributes for custom view you can define your own name.

引用

这篇关于Android版式-何时使用app:vs android :?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:58