Android Fragments 在方向更改时创建重复的 TextView

发布于 2024-12-29 10:50:22 字数 3852 浏览 3 评论 0原文

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

我正在制作一个带有 ActionBar 和 3 个选项卡的 Activity。选项卡下方是一个 FrameLayout,我将带有 TextView 的 Fragment 放置在其中。因此,当单击选项卡时,TextView 的内容应该发生变化。 在我改变方向之前,这一切都很好。更改后有一个重复的 TextView,我不知道它来自哪里。这是为了更好地理解的图片: 重叠的 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>

如果有人能告诉我我做错了什么,那就太好了。 提前致谢!

编辑:我已经尝试过 这个建议的解决方案 但我想使用类对象,以便我可以使用它们的方法。

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

I am trying to make a very simple App, but there's an error I can't get rid of it. Maybe someone can help me.

I'm making an Activity with an ActionBar and 3 Tabs. Beneath the Tabs is a FrameLayout, where I put Fragments with a TextView in it. So when clicking on a tab the content of the TextView shoud change.
This works fine until I change the orientation. After the change there is a duplicate TextView and I have no idea where it cames from. Here's a picture for better understanding:
Overlapping TextViews

Here's my 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();
    }

}

}

The Fragment's:

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>

and the content of the 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>

It would be amazing if some could show me what I'm making wrong.
Thank's in advance!

Edit: I tried already this proposed solution but I would like to work with class Objects, so that I can use their Methods.

Edit2: Solved the problem now. It was sufficient to add android:configChanges="keyboardHidden|orientation|screenSize" to my Activity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙味迷妹 2025-01-05 10:50:22

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


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

android:configChanges="keyboardHidden|orientation|screenSize"

Since the OP already solved his problem, but hasn't been active on SO for almost a year, here is an answer containing the solution:


Adding the following to the Activity solved the problem:

android:configChanges="keyboardHidden|orientation|screenSize"
奢欲 2025-01-05 10:50:22

如果您的布局之一有问题,您可能会得到重叠的视图。如果您有片段,这种情况似乎尤其会发生。例如,片段中的视图可以与包含布局中的视图重叠。

有时这可以通过清理项目来解决(如果您使用的是 Eclipse)。

You can get overlapping views if there is something wrong with one of your layouts. This seems to happen in particular if you have a fragment. For example, a View in the Fragment can overlap a View in the containing layout.

Sometimes this can be fixed by Cleaning your project (if you are using Eclipse).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文