0

在我的应用程序中,每当我的屏幕方向发生变化时,我都想显示适合屏幕的不同图像。我怎样才能做到这一点?

4

1 回答 1

1

在您的 Activity 中实现 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 回答