私のアプリケーションでは、画面の向きが変わるたびに、画面に収まるさまざまな画像を表示したいと考えています。どうやってやるの?
1 に答える
1
アクティビティにメソッド onConfigurationChanged を実装すると、向きを確認して UI の外観を変更できます。
パラメータ: newConfig.orientation は、現在の方向を示します。
次のようなものを試してください:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
ImageView header = (ImageView) this.findViewById(R.id.header);
if ( newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE ) {
header.setImageResource(R.drawable.header480);
}
else if ( newConfig.orientation == Configuration.ORIENTATION_PORTRAIT ) {
header.setImageResource(R.drawable.header320);
}
}
この種のコードを使用して、実際に画面サイズをリアルタイムで取得することもできます。
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
...
于 2010-12-10T09:20:00.823 に答える