【发布时间】: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