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

提前。

有帮助吗?

解决方案

在您的活动中实现了configuration的方法,您可以检查方向并更改UI的外观。

参数:newconfig.corientation将告诉您当前的方向。

尝试类似:

@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();
...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top