如何使用标准属性机器人:文字在我的自定义视图? [英] How to use standard attribute android:text in my custom view?

查看:111
本文介绍了如何使用标准属性机器人:文字在我的自定义视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了自定义视图扩展 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

这篇关于如何使用标准属性机器人:文字在我的自定义视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆