How to use standard attribute android:text in my custom view?

后端 未结 2 1352
礼貌的吻别
礼貌的吻别 2020-12-01 01:46

I wrote a custom view that extends RelativeLayout. My view has text, so I want to use the standard android:text without

2条回答
  •  时光取名叫无心
    2020-12-01 02:23

    use this:

    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

提交回复
热议问题