直接新建个工程,放到主Activity的onCreate方法中即可。
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.GREEN);
TextView tv = new TextView(this);
tv.setTextSize(30);
tv.setTextColor(Color.RED);
LinearLayout.LayoutParams lll = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
ll.addView(tv,lll);
DisplayMetrics dm= new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
String solution = ""+dm.widthPixels+"*"+dm.heightPixels;
tv.setText("当前屏幕的分辨率是:"+solution);
this.setContentView(ll);
}