如何从自定义视图中访问layout_height?

发布于 2024-09-15 11:31:57 字数 245 浏览 10 评论 0原文

我有一个自定义视图,我只想访问 layout_height 的 xml 布局值。

我目前正在获取该信息并将其存储在 onMeasure 期间,但这仅在首次绘制视图时发生。我的视图是 XY 图,它需要尽早知道其高度,以便可以开始执行计算。

该视图位于 viewFlipper 布局的第四页上,因此用户可能暂时不会翻转到它,但是当他们翻转到它时,我希望视图已经包含数据,这要求我有高度进行计算。

谢谢!!!

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!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

面犯桃花 2024-09-22 11:31:59

那行得通:)...您需要将“android”更改为“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..
}

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..
}
无尽的现实 2024-09-22 11:31:59

你可以使用这个:

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();
}

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();
}
疯了 2024-09-22 11:31:59

来自 public View(Context context, AttributeSet attrs) 构造函数文档:

构造函数被调用时
从 XML 扩充视图。这是
当视图正在被调用时
从 XML 文件构建,
提供的属性是
在 XML 文件中指定。

因此,为了实现您所需要的,请为您的自定义视图提供一个构造函数,该构造函数将 Attributes 作为参数,即:

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

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..
}
多像笑话 2024-09-22 11:31:59

Kotlin 版本

针对此问题编写的答案并未完全涵盖该问题。事实上,他们正在互相补足。总结一下答案,首先我们应该检查 getAttributeValue 返回值,然后如果 layout_height 定义为维度值,则使用 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)
    }
}

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)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文