简体   繁体   中英

get width, height from Layout

I need to get width and height of Linear layout, however I do not know when the Relative Layout is ready to provide me these two attributes.

but it only returns 0.

my screen 在此处输入图片说明

please check my code, i have no idea.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        int width = container.getWidth();
        int height = container.getWidth();

        Log.d("MainActivity onCreate", "width #" + width + ", height #" + height);
    }
}

and activity_main.xml layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:background="#FFFFFFAA"
        android:orientation="vertical"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</RelativeLayout>

how do i fix this?

Whenever you try to get dimensions on onResume() or on onCreate() method, it always returns 0 . So you should use ViewTreeObserver

ViewTreeObserver vto = container.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        container.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = container.getMeasuredHeight();
        finalWidth = container.getMeasuredWidth();
        return true;
    }
});

Also you can customize your View which will extends with LinearLayout and in onMeasure() method you can get width and height .

First of all replace

 int height = container.getWidth();

by

 int height = container.getHeight();

container.getHeight() will return 0 (as well as container.getWidth() ) if it's not drawed yet.

Solution would be to get the height after your view is layouted:

container.post(new Runnable(){
    public void run(){
     int height = container.getHeight();
    }
});

First, you are calling getWidth twice - for width and height.
You can try it with measureSpecs, like this:

int matchParentMeasureSpecWidth = View.MeasureSpec.makeMeasureSpec(((View) container.getParent()).getWidth(), View.MeasureSpec.EXACTLY);
int matchParentMeasureSpecHeight = View.MeasureSpec.makeMeasureSpec((View) container.getParent()).getHeight(), View.MeasureSpec.EXACTLY);
v.measure(matchParentMeasureSpecWidth, matchParentMeasureSpecHeight);

int height = container.getMeasuredHeight();

You can try and mesaure it first. But make sure you do so only after every other view as been added beacuse your LinearLayout is depended on other views.

LinearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
LinearLayout.getMeasuredWidth();

Try this

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

    final LinearLayout container = (LinearLayout)findViewById(R.id.container);

            container.post(new Runnable()
            {

                @Override
                public void run()
                {
                    Log.i("TEST", "  Layout width : "+ container.getWidth() +"  Layout height "+ container.getHeight()) ;

                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM