1

我同时使用使用 Activity 或不使用 Activity,在 Android 中没有同时获得高度和宽度。请纠正我,我做错了什么。

这里有活动:

public class LandPort extends Activity{

    public final static int height = 0;
    public final static int width = 0;
    Context context;
    DisplayMetrics displaymetrics = new DisplayMetrics();


    public LandPort(Context context) {
        this.context = context;
    }

    public int getHeight() {

        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int height = displaymetrics.heightPixels;
        return height;
    }

    public int getWidth() {
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels;
        return width;
    }

}

在 MainActivity 中:

LandPort lp= new LandPort(getApplicationContext());
        int height = lp.getHeight();
        int width =lp.getWidth();

高度和宽度都设为 0;

另一个没有活动:

public class GetHeigthWidth {

    public final static int height = 0;
    public final static int widht = 0;
    Context context;
    WindowManager wm = (WindowManager) context
            .getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    public GetHeigthWidth(Context context) {
        this.context = context;
    }

    public int getHeight() {

        @SuppressWarnings("deprecation")
        int height = display.getHeight();
        return height;
    }

    public int getWidth() {
        @SuppressWarnings("deprecation")
        int widht = display.getWidth();
        return widht;
    }

}

来自 MainActivity:

GetHeigthWidth ghw= new GetHeigthWidth(getApplicationContext());
        int height = ghw.getHeight();
        int width =ghw.getWidth();

Aslo 得到高度和宽度 0。

那么,如何在此获得实际高度和宽度>

4

3 回答 3

0

尝试这个

@Override
public void onCreate(Bundle savedInstanceState) {       

    super.onCreate(savedInstanceState);     

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);      

    setContentView(R.layout.yourLayout);
}

你需要初始化你的活动。

更新

你需要这样做:

import android.content.Context;
import android.view.Display;
import android.view.WindowManager;

public class GetHeigthWidth {

    public final static int height = 0;
    public final static int widht = 0;
    Context context;
    WindowManager wm;
    Display display;

    public GetHeigthWidth(Context context) {
        this.context = context;

        wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = wm.getDefaultDisplay();
    }

    public int getHeight() {

        @SuppressWarnings("deprecation")
        int height = display.getHeight();
        return height;
    }

    public int getWidth() {
        @SuppressWarnings("deprecation")
        int widht = display.getWidth();
        return widht;
    }

}
于 2013-05-24T09:04:41.497 回答
0
DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int width = displaymetrics.widthPixels;
于 2013-05-24T13:22:26.093 回答
0

打电话给你的 getHeight(); getWidth() 里面的东西

    int height;
    int width;
    ViewTreeObserver vto = view.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(globalListner);

    OnGlobalLayoutListener globalListner = new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
              height = view.getHeight();             
              widht = view.getWidth();
        }
    });

并且不要忘记在末尾删除该全局侦听器:)

于 2013-05-24T08:50:15.493 回答