【问题标题】:Change fragment but text reamin android更改片段,但文本 reamin android
【发布时间】:2017-04-30 16:26:29
【问题描述】:

我有这个问题:当我更改片段时,下面的 TextView 仍然存在,结果是 2 个 TextView。

我解释得更好:

我有一个带有字符串“hello word”的TextView

当我更改片段时,我想更改整个页面并设置另一个 TextView,但结果是 2 TextVIew

非常感谢!

activityMain.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer"/>

</android.support.v4.widget.DrawerLayout>

片段.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:gravity="center"
              android:background="#5ba4e5"
              android:layout_height="match_parent"
              android:id="@+id/fragment">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="40px"
        android:textColor="#ffffff"
        android:layout_gravity="center"
        android:id="@+id/fragmentText"
        />

</LinearLayout>

MainActivity 中的 this 用于更改片段:

private void selectItem(int id) {
        // update the main content by replacing fragments
        Fragment fragment = new MyFragment();

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    }

这是 MyFragment.java:

public class MyFragment extends Fragment {

    public MyFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment, container, false);
        // String planet = getResources().getStringArray(R.array.planets_array)[i];
        String planet = "prova fragment";
        int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                "drawable", getActivity().getPackageName());
        ((TextView) rootView.findViewById(R.id.fragmentText)).setText(planet);
        return rootView;
    }
}

【问题讨论】:

  • 你的代码是什么?如果您尝试有错误请发送错误...
  • 我尝试输入代码,但 StackOverflow 说:“代码太多”-.-
  • 我无法添加更多代码......
  • 是的..你必须先解释更多关于你的问题然后把你的代码
  • 您的问题标题不清楚...请更正!还有一件事:你在谷歌上搜索你的问题吗?

标签: java android android-fragments


【解决方案1】:

我使用:

fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

但是 content_frame 是 FrameLayout 而片段是 LinearLayout 所以我在 content_main.xml 中使用 LinearLayout 的 id 更改 content_frame 并且它可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多