Get “android:layout_height” from `AttributeSet` in a custom view constructor

来源:互联网 发布:php msgpackunpack 编辑:程序博客网 时间:2024/05/22 08:14

Get “android:layout_height” from `AttributeSet` in a custom view constructor

up vote0down votefavorite
share [fb]share [tw]

There seems to be alot of "similar" questions and answers to this scattered around which all refer to how to get a custom attribute from an AttributeSet. What I haven't been able to find so far is how to get an android namespace tag:

    <com.custom.view.StatusThumbnail        android:id="@+id/statusThumbnailContainer"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_margin="5dp"        android:layout_weight="1"/>

I would like to pull back the "layout_height" attribute from this custom component. Unfortunately from what I've read the closest I've got to how to do this is:

public StatusThumbnail(Context context, AttributeSet attrs) {    super(context, attrs);    String height = attrs.getAttributeValue("android", "layout_height");

But this returns null.

Surely this isn't a rare thing to try and do?

link|edit|flag

 add comment

2 Answers

activeoldestvotes
up vote1down voteaccepted

The namespace should be "http://schemas.android.com/apk/res/android" android is an alias declared in your xml file

link|edit|flag
 
 upvote flag
Genius. Fantastic - thanks very much. – Graeme 11 hours ago