布局在屏幕旋转时会自动复制 [英] Layout duplicating itself on screen rotation
问题描述
我有一个具有EditText和Button的布局.我< include>
在我的主布局中.
I have a layout that has an EditText and a Button. I <include>
it in my main layout.
我在布局和旋转方面遇到了一个怪异的问题.旋转设备(物理设备)时,它似乎在复制自身,弄乱了文本和布局.
I'm having a weird issue with the layout and rotation. It seems to duplicate itself when the device (physical) is rotated, messing up the text and layout.
在这里添加一些乱码后,它是第一次打开:
Here it is on first open, after I add some extra garble:
DSC_0013在片段启动时位于EditText中.
DSC_0013 is in the EditText on launch of the fragment.
然后,我旋转手机并添加一些不同的乱码:
Then, I rotate the phone and add some different garble:
您可以清楚地看到该问题.起初,我以为只是EditText搞砸了.但是,如果我添加了足够的文本以换行:
And you can see the issue pretty clearly. At first, I thought it was just the EditText messing up. But if I add enough text to make a new line:
我可以看到按钮也弄乱了.
I can see that the button gets messed up too.
我确实覆盖了 onSaveInstanceState
,但是其中我没有触摸EditText或它的值,它严格用于其他用途.
I do override onSaveInstanceState
, but in it I don't touch the EditText or its value, it's strictly used for something else.
发生了什么以及如何解决?
What's happening and how do I fix it?
推荐答案
已修复!
事实证明不是重复的视图,EditText或Button.这是整个片段.
Turns out it wasn't the view duplicating itself, or the EditText, or the Button. It was the entire fragment.
在活动的 onCreate
中,将片段添加到xml布局中:
In my Activity's onCreate
, I add the fragment to an xml layout:
private FileDetails fileDetailsFragment;
public void onCreate(Bundle savedInstanceState) {
...
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fileDetailsFragment = new FileDetails(fileData);
fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
fragmentTransaction.commit();
每次我旋转手机时,都会调用
onCreate
(按原意).因此,我检查了该活动是否是第一次运行,并且效果很好.
And onCreate
was being called every time I rotated the phone (as it's meant to). So I put in a check to see if the activity is being run for the first time, and it works great.
private FileDetails fileDetailsFragment;
public void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState == null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fileDetailsFragment = new FileDetails(fileData);
fragmentTransaction.add(R.id.DetailsHolder, fileDetailsFragment);
fragmentTransaction.commit();
} else {
fileDetailsFragment = (FileDetails) getSupportFragmentManager().findFragmentById(R.id.DetailsHolder);
}
这篇关于布局在屏幕旋转时会自动复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!