我有一个派生自TableLayout的自定义视图,并且我需要声明一个字符串属性Title,就像这样:
<com.mycompany.controls.NavigationControllerView
xmlns:app="http://schemas.usetech.com/apk/res/onroad"
title="@string/nav_title"
...
/>
(能够通过资源引用指定值)。我已经在 values/attrs.xml 文件中指定了该属性:
<declare-styleable name="NavigationControllerView">
<attr name="title" format="string"/>
...
</declare-styleable>
我的代码中我正在尝试:
public class NavigationControllerView extends TableLayout {
public NavigationControllerView(Context context, AttributeSet attrs) {
super(context, attrs);
View.inflate(getContext(), R.layout.inc_header, this);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.NavigationControllerView);
CharSequence title = a.getString(R.styleable.NavigationControllerView_title);
if (title != null)
setTitle(title.toString());
}
...
但是没有运气,标题总是空的。你看到我错在哪里了吗?