visited 13 states (5.77%)
Create your own visited map of The World or Triposo world travel guide for Android

2014-09-24

得到Android裝置的銀幕解析度

此程式主要是得到銀幕的解析度並顯示出來
example.java

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class example0305 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);   //全螢幕設定
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.main);

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

    TextView TextView1 = (TextView)findViewById(R.id.TextView01);
    TextView1.setText("手機銀幕大小為 "+metrics.widthPixels+" X "+metrics.heightPixels);
  }
}

main.xml

xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:id="@+id/TextView01"
android:textSize="20sp"
/>
</LinearLayout>

程式主要的重點在於 
import android.util.DisplayMetrics; 

DisplayMetrics metrics = new DisplayMetrics(); 

getWindowManager().getDefaultDisplay().getMetrics(metrics);

這些也是同標籤文章 :

沒有留言: