从布局获取宽度,高度

时间:2015-11-10 06:59:09

标签: android android-layout

我需要获得线性布局的宽度和高度,但我不知道相对布局何时准备好为我提供这两个属性。

但它只返回0.

我的屏幕 enter image description here

请检查我的代码,我不知道。

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

和activity_main.xml布局

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

我该如何解决这个问题?

5 个答案:

答案 0 :(得分:3)

每当您尝试在onResume()onCreate()方法上获取维度时,它始终会返回0。所以你应该使用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;
    }
});

此外,您可以自定义ViewLinearLayout将使用onMeasure()width方法进行自定义height// Geo - i got that numbers from google maps var content = [{ "geo-00":"geo:40.948823, -74.066191?q=40.948823, -74.066191?z=17", "geo-01":"geo:40.930027, -74.105579?q=40.930027, -74.105579?z=17", "geo-02":"geo:40.966902, -74.078574?q=40.966902, -74.078574?z=17", }]; // List var rules = [ {"tag":"a","href":"${geo-00}","class":"loc-link"}, {"tag":"a","href":"${geo-01}","class":"loc-link"}, {"tag":"a","href":"${geo-02}","class":"loc-link"}, ]; // JS Write var page = json2html.transform(content,rules);document.write(''+page+'');

答案 1 :(得分:2)

首先替换

 int height = container.getWidth();

通过

 int height = container.getHeight();

container.getHeight()如果尚未提取,则会返回0(以及container.getWidth())。

解决方案是在你的视图布局后获得高度:

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

答案 2 :(得分:1)

首先,您要调用两次getWidth - 宽度和高度 您可以使用measureSpecs进行尝试,如下所示:

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

答案 3 :(得分:1)

你可以先尝试一下它。但请确保只在添加了所有其他视图之后才这样做,因为您的LinearLayout依赖于其他视图。

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

答案 4 :(得分:1)

试试这个

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

                }
            });