【问题标题】:Android Fragments make duplicate TextView on orientation changeAndroid Fragments 在方向更改时重复 TextView
【发布时间】:2013-01-16 18:02:12
【问题描述】:

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

我正在制作一个带有 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();
    }

}

}

片段的:

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

【问题讨论】:

  • 我认为您应该自己添加解决方案作为答案并将其标记为已接受。

标签: android android-actionbar


【解决方案1】:

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


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

android:configChanges="keyboardHidden|orientation|screenSize"

【讨论】:

    【解决方案2】:

    如果您的某个布局有问题,您可能会得到重叠的视图。如果您有片段,这似乎尤其会发生。例如,Fragment 中的 View 可以与包含布局中的 View 重叠。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多