Android Fragments在方向更改时制作了重复的TextView
作者:互联网
我正在尝试制作一个非常简单的App,但是有一个错误无法消除.也许有人可以帮助我.
我正在使用ActionBar和3个选项卡进行活动.在选项卡的下面是一个FrameLayout,在其中放置了带有TextView的片段.因此,单击选项卡时,TextView的内容应更改.
直到我更改方向,这都可以正常工作.更改后,有一个重复的TextView,我不知道它来自哪里.这是一张更好理解的图片:
Overlapping TextViews
这是我的活动:
package com.test;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.widget.Toast;
public class ProblemActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String tab1 = "First Tab";
bar.addTab(bar
.newTab()
.setText(tab1)
.setTabListener(
new TabListener(new First())));
String tab2 = "Second Tab";
bar.addTab(bar
.newTab()
.setText(tab2)
.setTabListener(
new TabListener(new Second())));
String tab3 = "Third Tab";
bar.addTab(bar
.newTab()
.setText(tab3)
.setTabListener(
new TabListener(new Third())));
}
private class TabListener implements ActionBar.TabListener {
private MyFragment mFragment;
public TabListener(MyFragment fragment) {
this.mFragment = fragment;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.add(R.id.fragment_content, mFragment, null);
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(mFragment);
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(ProblemActivity.this, "Reselected!", Toast.LENGTH_SHORT)
.show();
}
}
}
片段的:
public class First extends MyFragment {
public First() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.tab1, container, false);
TextView tv = (TextView) fragView.findViewById(R.id.textView1);
tv.setText("First Tab");
return fragView;
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
以及Fragment’s.xml的内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView"
android:textSize="35dp" />
</LinearLayout>
如果有人能告诉我我做错了,那将是惊人的.
提前致谢!
编辑:我已经尝试了this proposed solution,但我想使用Object类,以便可以使用其Methods.
Edit2:现在解决了问题.将android:configChanges =“ keyboardHidden | orientation | screenSize”添加到我的Activity中就足够了.
解决方法:
由于OP已经解决了他的问题,但是已经有近一年没有活跃在SO上了,所以以下是包含解决方案的答案:
在“活动”中添加以下内容可解决此问题:
android:configChanges="keyboardHidden|orientation|screenSize"
标签:android-actionbar,android 来源: https://codeday.me/bug/20191201/2083615.html