如何使用标准属性机器人:文字在我的自定义视图?
问题描述:
我写了自定义视图扩展 RelativeLayout的
。我的看法有文字,所以我想用标准的安卓文本
没有需要指定一个<申报-styleable>
和不使用自定义命名空间的xmlns:XXX
每次我用我的自定义视图
I wrote a custom view that extends RelativeLayout
. My view has text, so I want to use the standard android:text
without the need to specify a <declare-styleable>
and without using a custom namespace xmlns:xxx
everytime I use my custom view.
这是个XML的,我用我的csutom观点:
this is th xml where I use my csutom view:
<my.app.StatusBar
android:id="@+id/statusBar"
android:text="this is the title" />
我如何绅士的属性值?我想我可以得到的android:文本属性
How can I gent the attribute value? I think I can get the android:text attribute with
TypedArray a = context.obtainStyledAttributes(attrs, ???);
但什么是 ???
在此情况下(无attr.xml一个设置样式)?
but what is ???
in this case (without a styleable in attr.xml)?
答
使用这样的:
public YourView(Context context, AttributeSet attrs) {
super(context, attrs);
int[] set = {
android.R.attr.background, // idx 0
android.R.attr.text // idx 1
};
TypedArray a = context.obtainStyledAttributes(attrs, set);
Drawable d = a.getDrawable(0);
CharSequence t = a.getText(1);
Log.d(TAG, "attrs " + d + " " + t);
a.recycle();
}
我希望你有一个想法
i hope you got an idea