【问题标题】:How do I access layout_height from within my custom view?如何从我的自定义视图中访问 layout_height?
【发布时间】:2011-04-02 07:55:33
【问题描述】:

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

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

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

谢谢!!!

【问题讨论】:

    标签: android android-layout layout custom-view


    【解决方案1】:

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

    在什么时候调用的构造函数 从 XML 膨胀视图。这是 在视图被调用时调用 从 XML 文件构造, 提供的属性是 在 XML 文件中指定。

    因此,为了实现您的需求,请为您的自定义视图提供一个以属性为参数的构造函数,即:

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

    【讨论】:

    【解决方案2】:

    工作:)...您需要将“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..
    }
    

    【讨论】:

    • 我试过 attrs.getAttributeValue("android","layout_width") 并返回 null。然后我尝试了你的解决方案,它奏效了。谢谢!
    • 但是,它仍然为我返回关于 layoutDirection 属性的 null!
    【解决方案3】:

    你可以用这个:

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

    【讨论】:

    • 获得高度后请务必致电a.recycle();
    【解决方案4】:

    Kotlin Version

    对此问题的回答并未完全涵盖该问题。其实,他们是在互相完善。总结答案,首先我们应该检查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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多