开发者

Different size images for different orientations

In my application, whenever the orientation of my screen changes, I want to display different imag开发者_如何学Pythones that will fit the screen. How can I do that?


Implement the method onConfigurationChanged in your Activity, there you can check the orientation and change the UI's look.

The parameter: newConfig.orientation will tell you the current orientation.

Try something like:

@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);
            
        }
    }

You can also use this kind of code to actually get the screen size in real time:

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();  
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜