How do I access layout_height from within my custom view?

邮差的信 提交于 2019-12-23 09:17:07

问题


I have a custom view and I simply wish to access the xml layout value of layout_height.

I am presently getting that information and storing it during onMeasure, but that only happens when the view is first painted. My view is an XY plot and it needs to know its height as early as possible so it can start performing calculations.

The view is on the fourth page of a viewFlipper layout, so the user may not flip to it for a while, but when they do flip to it, I would like the view to already contain data, which requires that I have the height to make the calculations.

Thanks!!!


回答1:


From public View(Context context, AttributeSet attrs) constructor docs:

Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file.

So to achieve what you need, provide a constructor to your custom view that takes Attributes as a parameter, i.e.:

public CustomView(final Context context, AttributeSet attrs) {
    super(context, attrs);
    String height = attrs.getAttributeValue("android", "layout_height");
    //further logic of your choice..
}



回答2:


that work :)... you need to change "android" for "http://schemas.android.com/apk/res/android"

public CustomView(final Context context, AttributeSet attrs) {
    super(context, attrs);
    String height = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height");
    //further logic of your choice..
}



回答3:


You can use this:

public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    int[] systemAttrs = {android.R.attr.layout_height};
    TypedArray a = context.obtainStyledAttributes(attrs, systemAttrs);
    int height = a.getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT);
    a.recycle();
}



回答4:


Kotlin Version

The answers which are written to this question do not completely cover the issue. Actually, they are completing each other. To sum up the answer, first we should check the getAttributeValue returning value, then if the layout_height defined as dimension values, retrieve it using getDimensionPixelSize:

val layoutHeight = attrs?.getAttributeValue("http://schemas.android.com/apk/res/android", "layout_height")
var height = 0
when {
    layoutHeight.equals(ViewGroup.LayoutParams.MATCH_PARENT.toString()) -> 
        height = ViewGroup.LayoutParams.MATCH_PARENT
    layoutHeight.equals(ViewGroup.LayoutParams.WRAP_CONTENT.toString()) -> 
        height = ViewGroup.LayoutParams.WRAP_CONTENT
    else -> context.obtainStyledAttributes(attrs, intArrayOf(android.R.attr.layout_height)).apply {
        height = getDimensionPixelSize(0, ViewGroup.LayoutParams.WRAP_CONTENT)
        recycle()
    }
}

// Further to do something with `height`:
when (height) {
    ViewGroup.LayoutParams.MATCH_PARENT -> {
        // defined as `MATCH_PARENT`
    }
    ViewGroup.LayoutParams.WRAP_CONTENT -> {
        // defined as `WRAP_CONTENT`
    }
    in 0 until Int.MAX_VALUE -> {
        // defined as dimension values (here in pixels)
    }
}


来源:https://stackoverflow.com/questions/3542456/how-do-i-access-layout-height-from-within-my-custom-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!