无法使.getwidth()和.getheight()工作

时间:2015-03-01 17:26:57

标签: java android height width

我知道在这个主题上还有很多其他问题,但是我已经查看了所有这些问题,但仍然没有让它发挥作用。

我已经为测试制作了这段代码:

public class MainActivity extends Activity {

RelativeLayout layout;
TextView widthtext;
TextView heighttext;
int width;
int height;

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

    layout = (RelativeLayout) findViewById(R.id.rela);

    widthtext = (TextView) findViewById(R.id.textView);
    heighttext = (TextView) findViewById(R.id.textView2);

    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            width = layout.getWidth();
            height = layout.getHeight();
        }
    });

    widthtext.setText(width);
    heighttext.setText(height);
}

<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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/rela">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="123"
    android:id="@+id/textView"
    android:layout_alignTop="@+id/textView2"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="123"
    android:id="@+id/textView2"
    android:layout_marginRight="50dp"
    android:layout_marginEnd="50dp"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

经过很多麻烦后,我现在已经注意到宽度和高度int的值是正确的,虽然我无法在TextViews中显示它们,但是获得了NullPointerExeption。

任何可以使这项工作的人?

EXTRA:

public class MainActivity extends Activity {

RelativeLayout layout;
TextView widthtext;
TextView heighttext;
int width;
int height;

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

    layout = (RelativeLayout) findViewById(R.id.rela);

    widthtext = (TextView) findViewById(R.id.textView);
    heighttext = (TextView) findViewById(R.id.textView2);

    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            width = layout.getWidth();
            height = layout.getHeight();
        }
    });

    widthtext.setText(width + "");
    heighttext.setText(height + "");
}

}

2 个答案:

答案 0 :(得分:5)

你这样做

widthtext.setText(width);
heighttext.setText(height);

在你的听众之外,所以它自然没有价值。

将它们移到里面

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        width = layout.getWidth();
        height = layout.getHeight();

        // move here
        widthtext.setText(width);
        heighttext.setText(height);
    }
});
}

blackbelt中指出comment,您正在呼叫setText()并传递int。这将查找该值的资源ID。将这些更改为Strings

widthtext.setText("" + width);
heighttext.setText("" + height);

答案 1 :(得分:0)

setText获取字符串值

所以而不是

setText(width);
setText(height);

DO

setText(width+"");
setText(height+"");