Android - get Height of the parent layout

2020-08-26 04:23发布

问题:

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()

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

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.

回答1:

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

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

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



回答2:

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.

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

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:

 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.



回答3:

Try something like this :

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


回答4:

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();
        .
        .
    }
}


回答5:

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

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

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


回答6:

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.



标签: android