获取Android中的布局的大小 [英] Getting the size of an layout in Android

查看:192
本文介绍了获取Android中的布局的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有定义以下布局:

 <&的LinearLayout GT;
  <的TextView />
  <&RelativeLayout的GT;
     <&TextView的GT;
     <按钮和GT;
     <按钮和GT;
  < / RelativeLayout的>
<&的LinearLayout GT;

现在我想要得到的RelativeLayout的高度(我要以编程方式指定其子女的身高):

 查看相对= findViewById(R.id.RelativeLayout01);
INT宽度= rel.getWidth();
INT高度= rel.getHeight();

返回的大小为0 。我如何获得RelativeLayout的大小(它主要是伪code以上,但两者都布局在XMLFILL_PARENT的高度/宽度)


解决方案

  

我如何获得RelativeLayout的大小


您code可能是好的,但时机是错误的。您也许执行该code在的onCreate(),这是太早了,因为小部件尚未在屏幕上放置了呢。

该负责人的回答是用于扩展 RelativeLayout的(或者,也许,的ViewGroup )和的实现自己的布局规则中,使用措施( )布局()

I have the following layout defined:

<LinearLayout>
  <TextView />
  <RelativeLayout>
     <TextView>
     <Button>
     <Button>
  </RelativeLayout>
<LinearLayout>

now i want to get the height of the RelativeLayout (i want to specify the height of its children programatically):

View rel = findViewById(R.id.RelativeLayout01);
int width = rel.getWidth();
int height = rel.getHeight();

but the size returned is 0. How do I get the size of the RelativeLayout (it's mostly pseudocode above, but both layout have a height/width of "fill_parent" in the xml)

解决方案

How do I get the size of the RelativeLayout

Your code is probably OK, but the timing is wrong. You are perhaps executing that code in onCreate(), which is too early, as the widgets have not been laid out on the screen yet.

The official answer is for you to extend RelativeLayout (or, perhaps, ViewGroup) and implement your own layout rules, using measure() and layout().

这篇关于获取Android中的布局的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆