onConfigurationChanged() 不会在方向改变时被调用
我在 SO 上看到了一些相关问题,但我无法解决我的问题。 我为肖像和风景模式创建了单独的布局。
我已经修改了 AndroidManifest.xml
文件以进行相应的方向更改,但是当我在活动中实现它时,onConfigurationChanged()
不起作用。
现在问题出在 onCreate()
中的 layout.addView(graphView, lp);
处。 我已经在 GraphView 类中编写了绝对硬编码值。
因此,它在 PORTRAIT 模式下完美工作,但当我切换到 LANDSCAPE 模式时,graphView 未正确放置。
为了解决这个问题,我创建了专门为 LANDSCAPE 模式编码的 GraphViewLand 类。但它没有被 onConfigurationChanged()
调用。
布局的其余部分非常完美,因为我为每个方向创建了单独的 main.xml
文件。但由于 graphView
是通过编程创建的,因此它的放置不正确。
我在这里做错了什么吗?
我刚刚从此处读到此内容:
对于任何类型的配置更改你说你在那里处理,你将收到对当前活动的 onConfigurationChanged(Configuration) 方法的调用,而不是重新启动。 如果配置更改涉及任何您不处理的内容,则 Activity 仍将重新启动,并且 onConfigurationChanged(Configuration) 将不会被调用。
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");
}
}
任何帮助:)
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.
I've modified the AndroidManifest.xml
file for corresponding orientation change but onConfigurationChanged()
is not working when I'm implementing it in activity.
Now the issue is at layout.addView(graphView, lp);
in onCreate()
.
I've written absolute hard-coded value in GraphView
class.
So it works perfectly foe PORTRAIT mode but graphView is not correctly placed when I switch to LANDSCAPE mode.
To resolve this I've created GraphViewLand
class which is coded exclusively for LANDSCAPE mode. But it is not getting called from onConfigurationChanged()
.
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.
Am I doing anything wrong here?
I just read this from here:
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() method
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() method
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");
}
}
Any help appriciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在 xml 中创建两种不同类型的视图(横向和纵向),并且必须在不同类型的视图上编写逻辑,则不要在清单中使用
android:configChanges
。如果清单中没有
android:configChanges
并且您有不同的横向和纵向布局集,那么当您更改方向时,您的控件将转到 onCreate() 在该方法中您可以编写逻辑。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.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 编码为 Activity 类中的覆盖?
基本覆盖:
我的信息清单声明:
Did you code onConfigurationChanged as an override in your Activity class?
Basic override:
My manifest declaration for info:
怎么说
onConfigurationChanged()
方法没有被调用呢?日志没有打印出来吗?不是有吐司吗?其次,您提到问题出在
onCreate()
中的layout.addView
处。请注意,如果您的清单中存在android:configChanges
,则在方向更改时不会调用onCreate()
。您需要将以编程方式修改 UI 布局的代码段移至onConfigurationChanged()
方法中。How do you say that the
onConfigurationChanged()
method is not called? Is the log not being printed? Isn't the toast being shown?Secondly, you mention that the issue is at
layout.addView
inonCreate()
. Do note that ifandroid:configChanges
is present in your manifest, thenonCreate()
is not called on orientation change. You need to move the piece of code that programmatically modifies the UI layout into theonConfigurationChanged()
method.