我须要实现本身的属性,例如com.android.R.attr
java
在官方文档中找不到任何内容,所以我须要有关如何定义这些属性以及如何从个人代码中使用它们的信息。 android
Qberticus的回答很好,可是缺乏一个有用的细节。 若是要在库中实现这些,请替换: app
xmlns:whatever="http://schemas.android.com/apk/res/org.example.mypackage"
与: 框架
xmlns:whatever="http://schemas.android.com/apk/res-auto"
不然,使用该库的应用程序将出现运行时错误。 ide
上面的答案涵盖了全部细节,除了两点。 函数
首先,若是没有样式,则将使用(Context context, AttributeSet attrs)
方法签名实例化首选项。 在这种状况下,只需使用context.obtainStyledAttributes(attrs, R.styleable.MyCustomView)
便可获取TypedArray。 布局
其次,它没有涵盖如何处理资源(数量字符串)。 这些不能使用TypedArray处理。 这是个人SeekBarPreference的代码片断,用于设置首选项摘要,并根据首选项的值将其值格式化。 若是首选项的xml将android:summary设置为文本字符串或字符串源,则将首选项的值格式化为字符串(应该在其中包含%d,以获取该值)。 若是android:summary设置为plaurals资源,则用于格式化结果。 ui
// Use your own name space if not using an android resource. final static private String ANDROID_NS = "http://schemas.android.com/apk/res/android"; private int pluralResource; private Resources resources; private String summary; public SeekBarPreference(Context context, AttributeSet attrs) { // ... TypedArray attributes = context.obtainStyledAttributes( attrs, R.styleable.SeekBarPreference); pluralResource = attrs.getAttributeResourceValue(ANDROID_NS, "summary", 0); if (pluralResource != 0) { if (! resources.getResourceTypeName(pluralResource).equals("plurals")) { pluralResource = 0; } } if (pluralResource == 0) { summary = attributes.getString( R.styleable.SeekBarPreference_android_summary); } attributes.recycle(); } @Override public CharSequence getSummary() { int value = getPersistedInt(defaultValue); if (pluralResource != 0) { return resources.getQuantityString(pluralResource, value, value); } return (summary == null) ? null : String.format(summary, value); }
onDialogClosed
方法中调用notifyChanged()
。 当前,最好的文档是源。 您能够在这里(attrs.xml)进行查看。 this
您能够在顶部<resources>
元素中或<declare-styleable>
元素内部定义属性。 若是要在多个位置使用attr,请将其放在根元素中。 注意,全部属性共享相同的全局名称空间。 这意味着,即便您在<declare-styleable>
元素内建立一个新属性,也能够在其外部使用它,而且您不能建立具备相同名称,不一样类型的另外一个属性。 spa
<attr>
元素具备两个xml属性name
和format
。 name
使您能够称呼它,这就是您最终在代码中引用它的方式,例如R.attr.my_attribute
。 format
属性能够具备不一样的值,具体取决于所需属性的“类型”。
您可使用|
将格式设置为多种类型|
,例如format="reference|color"
。
enum
属性能够定义以下:
<attr name="my_enum_attr"> <enum name="value1" value="1" /> <enum name="value2" value="2" /> </attr>
flag
属性是类似的,除了须要定义值,以即可以将它们位在一块儿:
<attr name="my_flag_attr"> <flag name="fuzzy" value="0x01" /> <flag name="cold" value="0x02" /> </attr>
除了属性以外,还有<declare-styleable>
元素。 这使您能够定义自定义视图可使用的属性。 您能够经过指定<attr>
元素来执行此操做,若是先前已定义该元素,则不指定format
。 若是您想重用android attr,例如android:gravity,则可使用name
,以下所示。
自定义视图<declare-styleable>
:
<declare-styleable name="MyCustomView"> <attr name="my_custom_attribute" /> <attr name="android:gravity" /> </declare-styleable>
在自定义视图上以XML定义自定义属性时,您须要作一些事情。 首先,您必须声明一个名称空间以找到您的属性。 您能够在根布局元素上执行此操做。 一般只有xmlns:android="http://schemas.android.com/apk/res/android"
。 如今,您还必须添加xmlns:whatever="http://schemas.android.com/apk/res-auto"
。
例:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:whatever="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <org.example.mypackage.MyCustomView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" whatever:my_custom_attribute="Hello, world!" /> </LinearLayout>
最后,要访问该自定义属性,一般能够在自定义视图的构造函数中进行以下操做。
public MyCustomView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomView, defStyle, 0); String str = a.getString(R.styleable.MyCustomView_my_custom_attribute); //do something with str a.recycle(); }
结束。 :)
传统方法充满了样板代码和笨拙的资源处理。 这就是为何我制做了Spyglass框架 。 为了演示其工做原理,下面的示例显示了如何制做显示String标题的自定义视图。
步骤1:建立一个自定义视图类。
public class CustomView extends FrameLayout { private TextView titleView; public CustomView(Context context) { super(context); init(null, 0, 0); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs, 0, 0); } public CustomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(attrs, defStyleAttr, 0); } @RequiresApi(21) public CustomView( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); init(attrs, defStyleAttr, defStyleRes); } public void setTitle(String title) { titleView.setText(title); } private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) { inflate(getContext(), R.layout.custom_view, this); titleView = findViewById(R.id.title_view); } }
步骤2:在values/attrs.xml
资源文件中定义一个字符串属性:
<resources> <declare-styleable name="CustomView"> <attr name="title" format="string"/> </declare-styleable> </resources>
步骤3:将@StringHandler
批注应用于setTitle
方法,以告知Spyglass框架在视图放大时将属性值路由到此方法。
@HandlesString(attributeId = R.styleable.CustomView_title) public void setTitle(String title) { titleView.setText(title); }
如今您的类具备Spyglass批注,Spyglass框架将在编译时对其进行检测并自动生成CustomView_SpyglassCompanion
类。
步骤4:在自定义视图的init
方法中使用生成的类:
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) { inflate(getContext(), R.layout.custom_view, this); titleView = findViewById(R.id.title_view); CustomView_SpyglassCompanion .builder() .withTarget(this) .withContext(getContext()) .withAttributeSet(attrs) .withDefaultStyleAttribute(defStyleAttr) .withDefaultStyleResource(defStyleRes) .build() .callTargetMethodsNow(); }
而已。 如今,当您从XML实例化类时,Spyglass伴随程序将解释属性并进行所需的方法调用。 例如,若是咱们增长如下布局,则setTitle
将被调用"Hello, World!"
做为论点。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:width="match_parent" android:height="match_parent"> <com.example.CustomView android:width="match_parent" android:height="match_parent" app:title="Hello, World!"/> </FrameLayout>
该框架不只限于字符串资源,还具备许多用于处理其余资源类型的不一样注释。 它还具备用于定义默认值和用于在方法具备多个参数的状况下传递占位符值的注释。
有关更多信息和示例,请查看Github存储库。
若是您省略了attr
元素的format
属性,则可使用它来引用XML布局中的类。
Refactor > Rename
做品 Find Usages
有效 不要在... / src / main / res / values / attrs.xml中指定format
属性
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyCustomView"> .... <attr name="give_me_a_class"/> .... </declare-styleable> </resources>
在某些布局文件中使用它... / src / main / res / layout / activity__main_menu.xml
<?xml version="1.0" encoding="utf-8"?> <SomeLayout xmlns:app="http://schemas.android.com/apk/res-auto"> <!-- make sure to use $ dollar signs for nested classes --> <MyCustomView app:give_me_a_class="class.type.name.Outer$Nested/> <MyCustomView app:give_me_a_class="class.type.name.AnotherClass/> </SomeLayout>
在您的视图初始化代码中解析类... / src / main / java /.../ MyCustomView.kt
class MyCustomView( context:Context, attrs:AttributeSet) :View(context,attrs) { // parse XML attributes .... private val giveMeAClass:SomeCustomInterface init { context.theme.obtainStyledAttributes(attrs,R.styleable.ColorPreference,0,0).apply() { try { // very important to use the class loader from the passed-in context giveMeAClass = context::class.java.classLoader!! .loadClass(getString(R.styleable.MyCustomView_give_me_a_class)) .newInstance() // instantiate using 0-args constructor .let {it as SomeCustomInterface} } finally { recycle() } } }