【问题标题】:how to programmatically get the height of a view with buttons on it?如何以编程方式获取带有按钮的视图的高度?
【发布时间】:2012-02-16 10:21:48
【问题描述】:

好的,这是我的问题,我想创建一个滚动视图,该视图填充到位于屏幕底部的一组两个按钮。知道我不能只将滚动视图设置为填充父级,我想我会去获取带有按钮的线性布局的高度,然后是我设置为填充父级的滚动视图的高度。然后从另一个中减去一个,然后重置滚动视图高度。我想出了如何获取和设置滚动视图高度。我找到了一种方法来测量活动中的所有其他视图,但是对于这个,它总是返回零,我不知道如何获得它的高度。

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>

【问题讨论】:

  • 只需使用RelativeLayout 而不是LinearLayout,这样您就可以轻松地以您描述的方式定位元素,而无需进行任何讨厌的测量等。

标签: android android-layout button height


【解决方案1】:

我终于自己弄明白了 如果小部件没有出现在屏幕上,它们就不会被创建。因此按钮元素仍然为零,因为它们从未渲染过;而且它们从未渲染过,因为滚动视图将它们推离了屏幕。 所以我这次从不同的角度解决了这个问题,我所做的是将滚动视图设置为任意小的数字,以便按钮留在屏幕上,将存储的顶级布局的大小存储为“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);
}

希望这可以帮助也解决同样问题的人

【讨论】:

    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    相关资源
    最近更新 更多