我需要实现自己的属性,如in com.android.R.attr
在官方文档中找不到任何内容,因此我需要有关如何定义这些attrs以及如何在我的代码中使用它们的信息.
我知道可以创建自定义UI元素(通过View或特定的UI元素扩展).但是有可能为新创建的UI元素定义新的属性或属性(我的意思是不继承,但是全新定义一些我无法用默认属性或属性处理的特定行为)
例如,我的自定义元素:
<com.tryout.myCustomElement
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Element..."
android:myCustomValue=<someValue>
/>
Run Code Online (Sandbox Code Playgroud)
那么可以定义MyCustomValue吗?
谢谢
我总是觉得有点难以向其他人解释:为什么存在XML命名空间?我们什么时候应该使用它们,何时不应该使用它们 在XML中使用命名空间时常见的陷阱是什么?
另外,它们如何与XML模式相关?XSD架构是否应始终与命名空间相关联?
以这一行为例:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
</configuration>
Run Code Online (Sandbox Code Playgroud)
一些简短的研究告诉我,不一定必须在该URL上托管.因此,如果它只是对本地程序集中包含的命名空间的引用,为什么要使用URL而不是像.NET(System.Data)通常使用的那些常规命名空间?
此问题已解决,请参阅注释以获取详细信息.
我正在扩展现有的Android View并加载一些自定义属性,如使用XML声明自定义Android UI元素和定义自定义attrs中所述.
具有布尔和整数格式的属性可以正常工作,但是当我尝试指定对数组资源的引用时,应用程序在启动时崩溃.我在xml资源文件中定义了一个整数数组,我试图将它用作自定义视图的属性.
我可以使用数组资源来设置android Spinner类的"entries"属性而没有错误,所以它似乎是我的实现中的一个问题.logcat消息似乎没有提供有关崩溃的任何具体信息,但我仍然在寻找,所以如果我找到了什么,我会更新.
属性由(在attrs.xml中)声明:
<declare-styleable name="CustomView">
<attr name="values" format="reference"/>
<attr name="isActive" format="boolean"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)
该数组定义为(在arrays.xml中):
<integer-array name="nums">
<item>1</item>
<item>2</item>
<item>3</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud)
我通过以下方式引用数组:
<com.test.CustomView cv:values="@array/nums" />
Run Code Online (Sandbox Code Playgroud)
这会导致应用程序立即崩溃.此外,如果我引用颜色资源而不是数组,那么应用程序不会崩溃.有人知道如何处理这个问题吗?
假设我有一个扩展ViewGroup的类
public class MapView extends ViewGroup
Run Code Online (Sandbox Code Playgroud)
它包括在布局map_controls.xml像这样
<com.xxx.map.MapView
android:id="@+id/map"
android:background="@drawable/address"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</com.xxx.map.MapView>
Run Code Online (Sandbox Code Playgroud)
如何从AttributeSet中检索构造函数中的属性?让我们说背景场中的drawable.
public MapView(Context context, AttributeSet attrs) {
}
Run Code Online (Sandbox Code Playgroud) 我知道如何为视图创建自定义属性,例如:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SomeViewsCustomAttrs">
<attr name="someAttr" format="dimension" />
<attr name="anotherAttr" format="boolean" />
</declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)
我想知道是否有办法向这些自定义attrs添加文档.在XML编辑器中编辑布局时,您可以获得描述attrs内容的工具提示.例如,如果您键入android:layout_width=它,它将为您提供以下信息,"指定视图的基本宽度.[dimension,enum]"
有没有办法为你自己的attrs提供?
谢谢
所以我环顾四周,发现它android.R.styleable不再是SDK的一部分,即使它仍然在这里记录.
如果清楚地记录了备选方案的内容,那就不会成为问题.例如,AOSP日历应用程序仍在使用android.R.styleable
// Get the dim amount from the theme
TypedArray a = obtainStyledAttributes(com.android.internal.R.styleable.Theme);
lp.dimAmount = a.getFloat(android.R.styleable.Theme_backgroundDimAmount, 0.5f);
a.recycle();
Run Code Online (Sandbox Code Playgroud)
那么如果backgroundDimAmount没有得到它int[],怎么会得到android.R.styleable.Theme?
obtainStyledAttributes(int [])为了使其与SDK一起使用,我需要注意什么?
我想在我的Android应用程序中创建一个自定义控件.它将是一个圆形控件,带有一些较小的可移动圆圈,表示特定值.
有没有办法实现这一点?
是否至少有一些很好的教程,我可以阅读如何创建自定义控件?
android ×8
android-xml ×3
xml ×3
attr ×1
attributes ×1
controls ×1
namespaces ×1
schema ×1
url ×1
xsd ×1