若要獲得當前使用的手機分辨率,可使用以下程式碼:
ScreenActivity.java
public class ScreenActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得手機窗口
WindowManager manager = getWindowManager();
Display display = manager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
//使用TextView物件來顯示長寬值
TextView width = (TextView)findViewById(R.id.width);
TextView height = (TextView)findViewById(R.id.height);>
width.setText("宽度:"+ screenWidth);
height.setText("高度:"+ screenHeight);
}
}
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:id="@+id/width"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
沒有留言:
張貼留言