onConfigurationChanged()不会被调用的方向变化 [英] onConfigurationChanged() is not being called on orientation change

查看:356
本文介绍了onConfigurationChanged()不会被调用的方向变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到所以一些相关的问题,但我不能够解决我的问题。
我创建单独的布局纵向和横向模式。

I saw some related questions on SO, but I'm not able to resolve my issue. I've created separate layout for PORTRAIT and LANDSCAPE mode.

我修改了的Andr​​oidManifest.xml 文件进行相应方向的变化,但 onConfigurationChanged()不工作时,我实施它的活动。

I've modified the AndroidManifest.xml file for corresponding orientation change but onConfigurationChanged() is not working when I'm implementing it in activity.

现在的问题是,在 layout.addView(graphView,LP); 的onCreate()
我在 GraphView书面绝对硬codeD值类。

Now the issue is at layout.addView(graphView, lp); in onCreate(). I've written absolute hard-coded value in GraphView class.

所以它完美敌人人像模式,但graphView当我切换到横向模式放置不正确。

So it works perfectly foe PORTRAIT mode but graphView is not correctly placed when I switch to LANDSCAPE mode.

要解决这一点,我已经创建 GraphViewLand 类,这是codeD专门为横向模式。但它没有得到从 onConfigurationChanged称为()

To resolve this I've created GraphViewLand class which is coded exclusively for LANDSCAPE mode. But it is not getting called from onConfigurationChanged().

布局的其余部分是完美的到来,因为我已经创建单独的的main.xml 文件中每个方向。但是,由于 graphView 的编程方式创建,它不妥善安置。

Rest of the layout is coming perfectly as I've created separate main.xml file for each orientation. But since graphView is created programmatically, it is not placed properly.

我在做什么错在这里?

我刚刚从<读此href=\"http://developer.android.com/reference/android/app/Activity.html#getLastNonConfigurationInstance\"相对=nofollow>这里:

有关你说你处理没有任何类型的配置更改,您将收到一个电话到你当前活动的onConfigurationChanged(配置)方法,而不是重新启动。 如果配置变化涉及任何你不处理,但是,该活动仍然会重新启动并onConfigurationChanged(配置)将不会被调用。

For any types of configuration changes you say that you handle there, you will receive a call to your current activity's onConfigurationChanged(Configuration) method instead of being restarted. If a configuration change involves any that you do not handle, however, the activity will still be restarted and onConfigurationChanged(Configuration) will not be called.

onCreate()方法

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.MainLayout);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_TOP, btnindex.getId());

    GraphView graphView = new GraphView(this, values, "CurrentGraph",
                horlabels, verlabels);
    layout.addView(graphView, lp);
    }

onConfigurationChanged()方法

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();

Log.v("LAND", "SCPAE");
    }
}

任何帮助appriciated:)

推荐答案

如果您创建两种不同类型的视图(横向和纵向)的XML,你必须写不同类型的视图逻辑然后DONOT使用的android:configChanges 在清单

If you create two different types of view (for landscape and portrait) in xml and you have to write logic on different types of view then donot use android:configChanges in manifest.

如果你没有的android:configChanges 清单中,当你改变方向的控制会你有横向和纵向再套不同的布局来的onCreate()这个方法里面可以写你的逻辑。

If you have no android:configChanges in manifest and you have different sets of layout for landscape and portrait then when you change orientation your control will come to onCreate() inside that method you can write your logic.

这篇关于onConfigurationChanged()不会被调用的方向变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆