首页 > 解决方案 > 奇怪的情况:来自 MainActivity 的文本显示在每个片段中

问题描述

我是新来的,我正在写这个问题,因为我正在为 Android 设备制作我的第一个应用程序。当然,我使用 Android Studio。我的问题是:

我遇到了一个奇怪的情况:我在 MainActivity 中有一个对用户表示欢迎的 TextVield,它在应用程序启动时显示,当我使用汉堡菜单导航到我的应用程序中的其他片段时,来自 MainActivity 的文本在每个片段中都可见。汉堡包本身就很好用。我能做些什么来解决这个问题?

以下是我的部分代码——我认为是关键部分,但我不能 100% 确定,因为我对 Android 和 Java 还很陌生。

// 这来自 MainActivity.java :

public void selectedtMenuItem(MenuItem menuItem)
    {
        android.support.v4.app.Fragment mfragment= null;
        Class fragmentClass;

        switch (menuItem.getItemId())
        {
            case R.id.main:
                fragmentClass = main.class;    
                break;
            case R.id.menu:
                fragmentClass = menu.class;    
                break;
            case R.id.galery:
                fragmentClass = galery.class;  
                break;
            case R.id.contact:
                fragmentClass = contact.class;  
                break;
            default:
                fragmentClass = main.class;   
        }
        try
        {
            mfragment= (android.support.v4.app.Fragment) fragmentClass.newInstance();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();

        fragmentManager.beginTransaction().replace(R.id.fragmentContent, mfragment).addToBackStack(null).commit();
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mlayout.closeDrawers();

    }

    private void setContent (NavigationView navigationView)
    {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
        {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item)
            {
                selectedtMenuItem(item);
                return false;
            }
        });
    }

// 这来自 activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_activity"
    tools:context=".MainActivity">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentContent" >

        <TextView
            android:id="@+id/welcome"
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_gravity="center"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
         />


    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nawigacja"
        app:headerLayout="@layout/header"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:itemTextColor="@color/black"
        app:itemIconTint="#303F9F"
        app:menu="@menu/drawermenu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

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

// 也许 AndroidManifest.xml 会很有用:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/logo"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

先感谢您!

标签: javaandroidandroid-fragmentstextfieldmain-activity

解决方案


这相当简单:您正在做的是将片段添加到ConstraintLayout您拥有的内容中。它不会删除TextView您拥有的,而只是在其后添加片段的 UI 元素。

你应该做些什么来保留你的设计:

  1. 添加另一个元素,FrameLayout然后TextBox将您的 idfragmentContent从您的ConstraintLayout

    <TextView
        android:id="@+id/welcome"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp" />
    
     <FrameLayout
        android:id="@+id/fragmentContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    

  2. 对它进行交易,就像你已经在做的那样:

    fragmentManager.beginTransaction().replace(R.id.fragmentContent, mfragment).addToBackStack(null).commit();
    

推荐阅读