取得视野高度

时间:2018-07-25 09:06:32

标签: android xml kotlin

我想从另一个View文件中获得一个xml高度。这些我总是得到0作为高度:

fun getHeight(): Int {
    val factory = LayoutInflater.from(context)
    val inflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val customView = inflater.inflate(R.layout.custom_expandable_helper,null)
    val relativeLayout = customView.findViewById<RelativeLayout>(R.id.container_height)
    val textView = customView.findViewById<TextView>(R.id.tv_height)
    textView.text = complaintText
    val rlp = RelativeLayout(activity)
    rlp.addView(customView)

    activity?.window?.decorView!!.viewTreeObserver.addOnGlobalLayoutListener(object : OnGlobalLayoutListener{
        override fun onGlobalLayout() {
            val obs = relativeLayout.viewTreeObserver
            viewHeight = relativeLayout.height
            obs.removeOnGlobalLayoutListener(this)
        }
    })

    val hei = rlp.height
    relativeLayout.post {
        viewHeight = relativeLayout.height
    }
    return relativeLayout.height
}

在此代码中,您可以看到许多获得高度的镜头,所有镜头均为0。

这就是我要夸大的xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:id="@+id/container_height">

<TextView
    android:id="@+id/tv_height"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/et_sides"
    android:text="sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample.sample."/>

3 个答案:

答案 0 :(得分:0)

您应该使用OnGlobalLayoutListener来侦听视图布局的更改,因为当您放大视图时,尚未测量其大小。

P.S。 抱歉,例如在JAVA中

var x ={};



(function(){

  //  var x=  {}
        x.disp3 = "hello i am from display3";
        x.display3 = function(){

            console.log(x.disp3)
        }
    }());

    (function(){
      //  var x=  {}
        x.disp2 = "hello i am from display2"
        x.display2 = function(){
            console.log(x.disp2)
        }
        console.log("call in invkFunc2");
       //x.display3();
    }());

(function(){
    //var x=  {}
    x.disp1 = "hello i am from display1"
    x.display1 = function(){
        console.log(x.disp1)
    }
}());

x.display1()
x.display2();
x.display3();

答案 1 :(得分:0)

视图仅在布局好后会返回非零的高度或宽度。这就是为什么我们使用ViewTreeObserver.OnPreDrawListenerViewTreeObserver.OnGlobalLayoutListener等的原因。

现在,该方法将不起作用,因为您只是返回始终为零的return relativeLayout.height

换句话说,代码中唯一可以获取视图高度的部分是:

activity?.window?.decorView!!.viewTreeObserver.addOnGlobalLayoutListener(object : 
OnGlobalLayoutListener{
        override fun onGlobalLayout() {
            val obs = relativeLayout.viewTreeObserver
            viewHeight = relativeLayout.height
            obs.removeOnGlobalLayoutListener(this)
        }
    })

如果您在其中登录viewHeight,该标签应打印正确的视图高度。

尝试

    val vto = activity?.window?.decorView?.viewTreeObserver?
    vto?.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
                override fun onPreDraw(): Boolean {
                vto?.removeOnPreDrawListener(this)
                val viewHeight = relativeLayout.height
                Log.v("logtag", viewHeight)
            }
        })

如果您不打算加载该布局,请执行以下操作:

val view = yourView
val widthSpec = View.MeasureSpec.makeMeasureSpec(parent.width, View.MeasureSpec.EXACTLY)
val heightSpec = View.MeasureSpec.makeMeasureSpec(parent.height, View.MeasureSpec.UNSPECIFIED)

val childWidthSpec = ViewGroup.getChildMeasureSpec(widthSpec, parent.paddingLeft+parent.paddingRight,view.layoutParams.width)
val childHeightSpec = ViewGroup.getChildMeasureSpec(heightSpec, parent.paddingTop+parent.paddingBottom, view.layoutParams.height)

view.measure(childWidthSpec, childHeightSpec)

答案 2 :(得分:0)

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth, parentHeight);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}