Android 获取视野高度
我想从另一个Android 获取视野高度,android,xml,kotlin,Android,Xml,Kotlin,我想从另一个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
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
}
fun getHeight():Int{
val factory=LayoutInflater.from(上下文)
val充气器=context?.getSystemService(context.LAYOUT\u充气器\u SERVICE)作为LayoutInflater
val customView=充气机。充气(R.layout.custom\u expandable\u helper,null)
val relativeLayout=customView.findViewById(R.id.container\U高度)
val textView=customView.findViewById(R.id.tv\U高度)
textView.text=投诉文本
val rlp=相对长度(活动)
rlp.addView(自定义视图)
activity?.window?.decorView!!.viewTreeObserver.addOnGlobalLayoutListener(对象:OnGlobalLayoutListener{
覆盖Loballayout(){
val obs=relativeLayout.viewTreeObserver
viewHeight=相对高度
obs.removeOnGlobalLayoutListener(此)
}
})
val hei=rlp高度
相对论{
viewHeight=相对高度
}
返回相对高度
}
在这段代码中,您可以看到许多获取高度的快照,它们都是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."/>
您应该使用OnGlobalYoutListener来监听视图布局的更改,因为当您放大视图时,其大小尚未测量 附言。 抱歉,例如在JAVA中
relativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// remove this once the layout measured
relativeLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
ViewGroup.LayoutParams lp = relativeLayout.getLayoutParams();
int height = lp.height; // Here we retrieve the measured height
}
});
视图在屏幕上布局后,仅返回非零高度或宽度。这就是为什么我们使用
ViewTreeObserver.onpredrawstener
,ViewTreeObserver.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)
视图高度是整数还是长?这是一个整数谢谢你的回答,但正如我在描述中所说的,给出高度的代码的所有部分都是零,包括
onGlobalLayout()
中的部分。但如果我误解了你的意思,请原谅。你是如何记录高度的?您是否像Log.v(“logtag”,tHeight())那样调用您的方法?因为如果你这样做的话,你会得到一个0的高度。试试活动?.window?.decorView?.viewTreeObserver?.addOnPreDrawListener(对象:viewTreeObserver.OnPreDrawListener{.override fun onPreDraw():布尔值{.activity?.window?.decorView?.viewTreeObserver?.removeOnPreDrawListener(这个)val viewHeight=relativeLayout.height Log.v(“lagtag”,viewHeight)})谢谢你,不幸的是它也给了我0。但是我认为你的想法接近解决方案,因为如果使用setContentView加载此视图,那么我就能够获得高度。哦,我想我误解了你的问题。因此,你没有夸大布局,因此,你应该首先测量它。谢谢你的回答!现在,当我在lp.height
处检查高度时,它不是0,而是-2,我预计是522。谢谢你的回答。我该怎么称呼这个?这个方法从视图父级继承,所以我应该创建一个继承视图的类?
@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);
}