如何以编程方式获得视图的高度与按钮上?(how to programmatically get the height of a view with buttons on it?)

编程入门 行业动态 更新时间:2024-10-12 16:29:57
如何以编程方式获得视图的高度与按钮上?(how to programmatically get the height of a view with buttons on it?)

好吧,这里是我的问题我想创建一个滚动视图,填充到屏幕底部的两个按钮集合。 知道我不能只设置滚动视图来填充父项我以为我会去获取线性布局的高度与它中的按钮,然后我设置填充父级的滚动视图的高度。 然后从另一个中减去一个,然后重置滚动视图高度。 我想出了如何获取和设置滚动视图的高度。 我发现了一种方法来测量活动中的所有其他视图,但是使用这个视图时,它总是返回一个零,并且我不知道如何获得它的高度。

final LinearLayout tv = (LinearLayout)findViewById(R.id.thebuttons); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { Integer grr = tv.getHeight(); Log.e("the h", grr.toString()); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });

现在,如果我将它设置为任何其他布局,我会根据我的预期获得它的高度,但将其设置为这一个,然后我得到0; 这是我正在测量的那个! 我也试图直接测量按钮无济于事

<LinearLayout android:id="@+id/thebuttons" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5sp" > <Button android:id="@+id/saveit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> <Button android:id="@+id/clearit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> </LinearLayout>

okay here's my issue I want to create a scroll view that fills down to a set of two buttons that are right at the bottom of the screen. knowing i can't just set the scroll view to fill parent I thought I'd go and get the height of the linear layout with the buttons in it and then the height of the scroll view that I set to fill parent. then subtract one from the other and then reset the scroll view height. I figured out how to get and set the scroll view height. and I found a way to that works to measure all of the other views in the activity but with this one it always returns a zero and I'm at a loss as to how to get the height of it.

final LinearLayout tv = (LinearLayout)findViewById(R.id.thebuttons); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { Integer grr = tv.getHeight(); Log.e("the h", grr.toString()); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });

now if I set it to any of my other layouts I get the height of it as I expect but set it to this one and I get 0; and this is the one I'm trying to measure! I've also tried to measure the button directly to no avail

<LinearLayout android:id="@+id/thebuttons" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5sp" > <Button android:id="@+id/saveit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> <Button android:id="@+id/clearit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> </LinearLayout>

最满意答案

如果这些小部件没有出现在屏幕上,他们不会被创建,我终于自己想出了它。 所以因此按钮元素仍然在零处,因为它们从未被渲染; 他们从来没有渲染,因为滚动视图将它们推离屏幕。 所以我从另一个角度攻击了这个问题,这次我将滚动视图设置为一个任意的小数字,这样按钮就会保持在屏幕上,并将顶层布局的大小存储到“int totalsize”中,然后我得到除了滚动视图之外的所有元素的大小,并获得每个视图的边距,并将它们总计为“int used”,然后将滚动视图高度设置为“totalsize-used”,这就是工作原理。 发现它我手动收集边距,当屏幕尺寸发生变化时边缘不起作用,边距也发生变化,因此发现它们与视图尺寸一起效果最好。

在我的创建方法中,我得到了这个:

final Button tv = (Button)findViewById(R.id.saveit); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) tv.getLayoutParams(); int btnsize =tv.getMeasuredHeight()+vlp.topMargin; sizeit(btnsize); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });

然后它的大小起作用

private void sizeit(Integer thebuttons) { View v = findViewById(R.id.viewmain); int total = v.getHeight(); v = findViewById(R.id.view1); ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) v.getLayoutParams(); int used=v.getHeight()+vlp.topMargin; v = findViewById(R.id.view2); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; v = findViewById(R.id.fonttext); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; v = findViewById(R.id.infolabel); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; Integer scrsize=total-used-thebuttons; v = findViewById(R.id.scrview); ScrollView scr = (ScrollView)findViewById(R.id.scrview); ViewGroup.LayoutParams scrlprams = scr.getLayoutParams(); scrlprams.height=scrsize; scr.setLayoutParams(scrlprams); }

希望这可以帮助那些同样面临同样问题的人

I finally Figured it out myself if the widgets do not appear on screen they do not get created. so thus the button elements where still at zero because they where never rendered; and they where never rendered because the scroll view pushed them off screen. so I attacked the problem from a different angle this time what I did was set the scroll view to an arbitrary small number so the buttons stay on screen, get the size of the top level layout stored that to "int totalsize", then I get the sizes of all the elements except the scroll view and got the margins for each view as well and totaled them all up into "int used", then I set the scroll view height to "totalsize-used" and that is what worked. found it I totaled up the margins manually that doesn't work when the screen size changes the margins also change so discovering them along with the view sizes works best.

in my on create method I got this:

final Button tv = (Button)findViewById(R.id.saveit); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) tv.getLayoutParams(); int btnsize =tv.getMeasuredHeight()+vlp.topMargin; sizeit(btnsize); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });

and then the size it function

private void sizeit(Integer thebuttons) { View v = findViewById(R.id.viewmain); int total = v.getHeight(); v = findViewById(R.id.view1); ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) v.getLayoutParams(); int used=v.getHeight()+vlp.topMargin; v = findViewById(R.id.view2); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; v = findViewById(R.id.fonttext); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; v = findViewById(R.id.infolabel); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; Integer scrsize=total-used-thebuttons; v = findViewById(R.id.scrview); ScrollView scr = (ScrollView)findViewById(R.id.scrview); ViewGroup.LayoutParams scrlprams = scr.getLayoutParams(); scrlprams.height=scrsize; scr.setLayoutParams(scrlprams); }

hope this helps someone also fighting with the same issue

更多推荐

本文发布于:2023-08-07 00:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459080.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   按钮   高度   方式   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!