範例程式如下:
package com.example.screenresolutiondemo; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.util.DisplayMetrics; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(getApplicationContext()); Resources res = getResources(); DisplayMetrics metrics = res.getDisplayMetrics(); tv.setText("螢幕資訊:\n"); tv.append("寬:" + metrics.widthPixels + "\n"); tv.append("高:" + metrics.heightPixels + "\n"); tv.append("DPI:" + metrics.densityDpi + "\n"); if (metrics.widthPixels < metrics.heightPixels) { tv.append("目前是直式\n"); } else { tv.append("目前是橫式\n"); } setContentView(tv); } }範例結果:
沒有留言:
張貼留言