search

Home  >  Q&A  >  body text

翻译 - Android平台如何获得屏幕尺寸?

我创建了一些自定义元素,想通过编程将它们放到右上角(距上部边缘n像素,右部边缘m像素),因此我需要知道屏幕具体的宽度和高度,然后确定具体的位置:

int px = screenWidth - m;
int py = screenWidth - n;

请问,在Activity如何获取屏幕的宽度和高度呢?

原问题:How to get screen dimensions

PHP中文网PHP中文网2773 days ago741

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 11:28:05

    Answer: Josef Pfleger
    (Best answer)
    If you want to get the pixel size of the screen, you can use getSize:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    

    If it is not in Activity, you can get the default value of Display through WINDOW_SERVICE:

    WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    

    If there is no getSize, you can use the getWidth and getHeight tools as follows:

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();  // deprecated
    int height = display.getHeight();  // deprecated
    

    Answer: Balaji.K
    I think it can be solved like this:

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();
    

    If it doesn’t work, you can try the following code: The first two lines are about DisplayMetrics code, which includes the content of width pixels and height pixels:

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    metrics.heightPixels;
    metrics.widthPixels;
    

    Answer: Francesco Feltrinelli
    What I'm talking about may not be the answer you need, but I think it's useful to know. If you need the size of the View, but the layout has not been set yet, and the relevant code is executed, you can use View.getViewTreeObserver().addOnGlobalLayoutListener() to create a ViewTreeObserver.OnGlobalLayoutListener, and then enter the code related to the size of the View, so that The relevant parameters of the layout can be corrected in time.


    Answer: Crbreingan
    To accomplish dynamic scaling using XML, you can use the attribute android:layout_weight. The following example is an improved version of synic's answer on this thread, which expresses the following: the button occupies 75% of the screen (weight = .25), and the text view occupies 25% (weight = .75).

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="somebutton">
    
        <TextView android:layout_width="fill_parent"
            android:layout_height="Wrap_content"
            android:layout_weight=".75">
    </LinearLayout>
    

    reply
    0
  • Cancelreply