开发者

Add custom string attribute in my custom android View

I have a cusom view derived from TableLayout, and I need to declare String property Title, like this:

<com.mycompany.controls.NavigationControlle开发者_高级运维rView
xmlns:app="http://schemas.usetech.com/apk/res/onroad"
title="@string/nav_title"
...
/>

(to be able specify value via resource reference). I've specified this property in values/attrs.xml:

<declare-styleable name="NavigationControllerView">
    <attr name="title" format="string"/>
    ...
</declare-styleable>

In my code I'm trying:

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());
}
...

but no luck, title is always null. Do you see where I'm wrong?


You should use

<com.mycompany.controls.NavigationControllerView
    xmlns:app="http://schemas.android.com/apk/res/onroad" 
    app:title="@string/nav_title" 
... /> 

do not miss app: prefix. Also correct namespace uri should be used.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜