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
every time I use my custom view.
this is the xml where I use my custom view:
<my.app.StatusBar android:id="@+id/statusBar" android:text="this is the title"/>
How can I get the attribute value? I think I can get the android:text attribute with
TypedArray a = context.obtainStyledAttributes(attrs, ???);
but what is ???
in this case (without a styleable in attr.xml)?
An <attr> element has two xml attributes name and format . name lets you call it something and this is how you end up referring to it in code, e.g., R. attr. my_attribute . The format attribute can have different values depending on the 'type' of attribute you want.
Keywords: Mobile Android Attribute. 1. declare-style leable defines some attributes, writes some identical attributes together, defines a name, or can be directly defined. In the following two ways, just use declare-style leable to organize similar attributes together.
Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With