onSizeChanged返回宽度,但不返回高度。

3

我需要获取我的View的高度和宽度,我已经按照在这里给出的示例使用子类化View并使用onSizeChanged(...)。 但是非常奇怪的是,onSizeChanged给我正确的宽度,但高度始终为0。这是我的子类:

public class MyView extends TextView {
int xMax=0; int yMax=0;Context c;

public MyView(Context context) {
    super(context);
    this.c=context;

}
@Override
   public void onSizeChanged(int w, int h, int oldW, int oldH) {

      xMax = w;
      yMax = h;

我的Activity类:

public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView dummyView = new MyView(this);

    LinearLayout ll = (LinearLayout) findViewById(R.id.mainmtc);
    ll.addView(dummyView);

我的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainmtc" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1"
androiad:orientation="vertical">
1个回答

2

会做的(我是StackOverflow的新手)。但我的问题的解决方案是什么?所有找到的示例都从未提到过这一点? - michaelsmith
我已经尝试过了,但高度仍然为0。当旋转设备时,宽度会发生变化,但高度不会。我需要获取设备屏幕的尺寸。我的方法正确吗? - michaelsmith
设备屏幕尺寸可以通过DisplayMetrics类获取。 - ChristopheCVB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接