Android Fragments在方向更改时制作重复的TextView

时间:2012-01-27 12:24:09

标签: android android-actionbar

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

我正在使用ActionBar和3个标签创建一个Activity。在Tabs下面是一个FrameLayout,我在其中放置了带有TextView的Fragments。因此,当单击选项卡时,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();
    }

}

}

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>

和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"添加到我的活动中就足够了。

2 个答案:

答案 0 :(得分:3)

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


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

android:configChanges="keyboardHidden|orientation|screenSize"

答案 1 :(得分:0)

如果您的某个布局出现问题,您可以获得重叠的观看次数。如果你有一个片段,这似乎特别发生。例如,片段中的视图可以与包含布局中的视图重叠。

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

相关问题