gpt4 book ai didi

Android Fragments 在方向更改时复制 TextView

转载 作者:太空狗 更新时间:2023-10-29 12:53:49 26 4
gpt4 key购买 nike

我正在尝试制作一个非常简单的应用程序,但出现了一个我无法消除的错误。也许有人可以帮助我。

我正在制作一个带有 ActionBar 和 3 个选项卡的 Activity 。 Tabs 下方是一个 FrameLayout,我在其中放置了带有 TextView 的 Fragment。因此,当单击选项卡时,TextView 的内容应该发生变化。这工作正常,直到我改变方向。更改后有一个重复的 TextView,我不知道它来自哪里。为了更好地理解,这是一张图片: Overlapping TextViews

这是我的 Activity :

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();
}

}

fragment 的:

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;
}

主.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但我想使用类对象,以便我可以使用它们的方法。

Edit2:现在解决了问题。将 android:configChanges="keyboardHidden|orientation|screenSize" 添加到我的 Activity 就足够了。

最佳答案

由于 OP 已经解决了他的问题,但是已经将近一年没有在 SO 上活跃了,这里是一个包含解决方案的答案:


将以下内容添加到 Activity 解决了问题:

android:configChanges="keyboardHidden|orientation|screenSize"

关于Android Fragments 在方向更改时复制 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9033209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com