Sunday, October 18, 2009

[android-developers] Re: Custom View - obtain android:layout_width value

fhucho wrote:
> I have a custom View, it is created from xml - how can I obtain the
> android:layout:width value in the constructor View(Context x,
> AttributeSet a) ?

To get at a custom attribute, you would use code like this:

TypedArray a=ctxt.obtainStyledAttributes(attrs,
R.styleable.Meter,
0, 0);

max=a.getInt(R.styleable.Meter_max, 100);
incrAmount=a.getInt(R.styleable.Meter_incr, 1);
decrAmount=-1*a.getInt(R.styleable.Meter_decr, 1);

a.recycle();

(from http://github.com/commonsguy/cw-advandroid/tree/master/Views/Meter/)

For android:layout_width, presumably you would use
android.R.styleable.ViewGroup_Layout_layout_width, though I have not
tried that.

However, in theory, that attribute should be handled for you, I would
think. You would negotiate the size in onMeasure().

--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

Android App Developer Training: http://commonsware.com/training.html

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments:

Post a Comment