Android - 获取父布局的高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3656139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:11:56  来源:igfitidea点击:

Android - get Height of the parent layout

android

提问by Robin

I want to determine the available height and width of the parent layout to which I have to add my view.

我想确定必须添加视图的父布局的可用高度和宽度。

I have used many methods on layout like

我在布局上使用了很多方法,比如

  • layout.getHeight()
  • layout.getRootView().getHeight()
  • 布局.getHeight()
  • layout.getRootView().getHeight()

All these methods return 0(zero) as the result.

所有这些方法都返回 0(零)作为结果。

My main requirement is to be able to give width to a view that is some % of the width of the layout .

我的主要要求是能够为视图赋予宽度为布局宽度的某些百分比。

Also I dont want to use tag for this. I want to do it through code.

我也不想为此使用标签。我想通过代码来做到这一点。

Thanks in advance.

提前致谢。

回答by Robin

Ok. I found a solution to it. The way we can get hieght of the screen in activity is

好的。我找到了解决方法。我们可以在活动中获得屏幕高度的方法是

getApplicationContext().getResources().getDisplayMetrics().heightPixels

getApplicationContext().getResources().getDisplayMetrics().heightPixels

But I still dont know how to get the pixel dimentions of parent layout?

但是我仍然不知道如何获取父布局的像素尺寸?

回答by Megha Joshi - GoogleTV DevRel

The issue here is you are trying to get the parent height/weight before the layoutmanager completes parent layout. You can only get parent dimensions, after its layout is complete.

这里的问题是您试图在 layoutmanager 完成父布局之前获取父高度/重量。您只能在布局完成后获取父维度。

The recommended way to do what you want to achieve is by using layout_weight field of LayoutParams. link text

推荐的方法是使用 LayoutParams 的 layout_weight 字段。 链接文字

In particular you should use:

特别是你应该使用:

LayoutParams lp = new LinearLayout.LayoutParams(width, height,
weight);

layout_weight value can be anything between 0.0 to 1.0. So for example if I have myButton within parentLayout, and I want myButton to occupy 30% of parent layout...I would use something like:

layout_weight 值可以是 0.0 到 1.0 之间的任何值。因此,例如,如果我在 parentLayout 中有 myButton,并且我希望 myButton 占据父布局的 30%...我会使用类似的东西:

 myButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                                               LayoutParams.WRAP_CONTENT,0.3));

The layout_weight sum of all child elements should be 1. Also, layout_weight should not be used with view's width or height set to LayoutParams.FILL_PARENT.

所有子元素的 layout_weight 总和应为 1。此外,layout_weight 不应与设置为 LayoutParams.FILL_PARENT 的视图的宽度或高度一起使用。

回答by lokoko

Try something like this :

尝试这样的事情:

view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        view.getHeight();
    }
  });

回答by Robeth

One alternative to programmatically implement this is to use following method:

以编程方式实现这一点的一种替代方法是使用以下方法:

public void onWindowFocusChanged(boolean b) { ...}

By the time this method is invoked, the layouting-process has been finished. Thus you can obtain the parent attributes correctly.

到调用此方法时,布局过程已完成。因此您可以正确获取父属性。

As Example:

例如:

public class A extends Activity {
    LinearLayout parent;
    Button child;
    public void onCreate(Bundle b) {
        parent = (LinearLayout) findViewById(R.id.linear1);
        child = (Button) findViewById(R.id.button1);
        .
        .
    }

@Override
public void onWindowFocusChanged(boolean b) {
        LayoutParams lpc = child.getLayoutParams();
        lpc.width = parent.getWidth();
        child.postInvalidate();
        .
        .
    }
}

回答by subaja

In OnCreate()method ,if you use layout.getHeight()and layout.getWidth(), it will returns 0, instead you can follow this:

OnCreate()方法中,如果您使用layout.getHeight()and layout.getWidth(),它将返回 0,您可以按照以下步骤操作:

public void onClick(View v) {   
    // TODO Auto-generated method stub

             int layWidth = Layout.getLeft();
             int layHeight = Layout.getHeight();
         break;     
    }

回答by trainz-are-kul

You are probobly calling getHeight()to early. Ensure that getParent().getHeight()is being called after viewGroup.addView(view0)and/or setContentView(activity_main.xml). This has happened to me in the past and this fixed it.

你可能是getHeight()提前打电话了。确保getParent().getHeight()viewGroup.addView(view0)和/或之后调用setContentView(activity_main.xml)。这在过去发生在我身上,这解决了它。