简体   繁体   English

我希望能够在 Android 中获取屏幕高度和宽度

[英]I want to be able to get the screen height and width in Android

I am trying to get the screen width and height so that I can make a button move randomly on the screen when I click it.我正在尝试获取屏幕宽度和高度,以便在单击按钮时可以在屏幕上随机移动按钮。

The code I have so far it: public class MainActivity extends AppCompatActivity {到目前为止,我拥有的代码是:public class MainActivity extends AppCompatActivity {

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

DisplayMetrics display = this.getResources().getDisplayMetrics();
int width = display.widthPixels;
int height = display.heightPixels;

public void moveMe(View view)
{
    Button myButton = (Button) findViewById(R.id.my_button);

    Random r = new Random();

    int x = (r.nextInt(width));
    int y = (r.nextInt(height));

    myButton.setX(x);
    myButton.setY(y);
}
}

The XML is: XML是:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
    android:id="@+id/my_button"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_x="0dp"
    android:layout_y="0dp"
    android:text="Yes"
    android:onClick="moveMe"/>
</AbsoluteLayout>

However, when I launch the app it says that it has stopped.但是,当我启动该应用程序时,它说它已停止。

The error I get in Android Studio is:我在 Android Studio 中得到的错误是:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test.test/com.example.test.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference

Thankyou for any help you can offer.感谢您提供的任何帮助。

You are calling getResources() before the super.onCreate(Bundle) is called.您在调用super.onCreate(Bundle)之前调用getResources() You must call getResources() after onCreate() .您必须在onCreate()之后调用getResources() onCreate() Move your code to onCreate or onStart :将您的代码移动到onCreateonStart

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DisplayMetrics display = getResources().getDisplayMetrics();
    int width = display.widthPixels;
    int height = display.heightPixels;
}

Try this inside onCreate :onCreate试试这个:

DisplayMetrics display = this.getResources().getDisplayMetrics();

int width = display.widthPixels;

int height = display.heightPixels

You can use code so that no need to weight till your layout get rendered completely.您可以使用代码,以便在布局完全呈现之前无需加权。

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

But in practice AbsoluteLayout is deprecated and its not good way to position Children using absolute positioning may not give same User Interface feel in all devices.Other option suggested by experts is RelativeLayout.但在实践中,AbsoluteLayout 已被弃用,它使用绝对定位来定位 Children 的不好方法可能不会在所有设备中提供相同的用户界面感觉。专家建议的其他选项是 RelativeLayout。

You can find downside of the AbsoluteLayout at the link Absolute positioning pitfalls您可以在链接绝对定位陷阱中找到AbsoluteLayout 的缺点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM