Java 从导航抽屉中打开新片段时,将content_main替换为

Java 从导航抽屉中打开新片段时,将content_main替换为,java,android,android-fragments,android-studio,fragment,Java,Android,Android Fragments,Android Studio,Fragment,我用谷歌搜索了一下,还没有找到答案,我使用的是Android导航抽屉模板,当从导航菜单中单击时,我有一些片段会相互替换,当应用程序第一次打开时,它会在内容主布局上打开,当我单击其中一个片段时,它会打开,但内容主布局中的内容仍在显示,我需要在使用其他片段时隐藏它,还需要制作一个“主页”按钮,以允许我从菜单返回此内容,是否需要帮助 问题是当我打开应用程序时,内容主布局会出现,当我滑动导航抽屉并在菜单中选择导航库时,音乐片段会打开,但内容主布局没有被替换 这是我的主要活动 package com.ju

我用谷歌搜索了一下,还没有找到答案,我使用的是Android导航抽屉模板,当从导航菜单中单击时,我有一些片段会相互替换,当应用程序第一次打开时,它会在内容主布局上打开,当我单击其中一个片段时,它会打开,但内容主布局中的内容仍在显示,我需要在使用其他片段时隐藏它,还需要制作一个“主页”按钮,以允许我从菜单返回此内容,是否需要帮助

问题是当我打开应用程序时,内容主布局会出现,当我滑动导航抽屉并在菜单中选择导航库时,音乐片段会打开,但内容主布局没有被替换

这是我的主要活动

package com.justmikey.justmik;

 import android.media.MediaPlayer;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v4.app.FragmentManager;
 import android.support.v7.internal.widget.ButtonBarLayout;
 import android.text.method.ScrollingMovementMethod;
 import android.view.View;
 import android.support.design.widget.NavigationView;
 import android.support.v4.view.GravityCompat;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.Button;
 import android.widget.TextView;

 import JustMikey.Fragments.MainFragment;
 import JustMikey.Fragments.MusicFragment;

 public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

//create a media player object called mp
MediaPlayer mp;
//declare my buttons play, pause and stop
Button play, pause,stop;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Set the textView scrollMain to scrollable
    TextView tv = (TextView) findViewById(R.id.scrollMain);
    tv.setMovementMethod(new ScrollingMovementMethod());

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open,      R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
     }




@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
     }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
     }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    android.app.FragmentManager fm = getFragmentManager();

    int id = item.getItemId();

    if (id == R.id.nav_camara) {

   fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();


    } else if (id == R.id.nav_gallery) {

        fm.beginTransaction().replace(R.id.content_frame, new MusicFragment()).commit();


    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
 }
这就是主要内容

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

    <ImageView
        android:layout_width="355dp"
        android:layout_height="wrap_content"
        android:src="@drawable/jmmain"
        android:id="@+id/imageView2"
        android:layout_gravity="center_horizontal|top"
        android:layout_alignRight="@+id/content_frame"
        android:layout_alignEnd="@+id/content_frame" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="274dp"
        android:text="hello hello hello hello hello hello hello hello hello  hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello "
        android:id="@+id/scrollMain"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_alignBottom="@+id/scrollView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="28dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:background="#ffffff"
        android:padding="10dp" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="center"></ScrollView>


</FrameLayout>

</RelativeLayout>

音乐版式

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



一个
AppCompatActivity
应该使用通过
getSupportFragmentManager()
检索的
FragmentManager
,现在回答这个问题已经太晚了,但我只是为了以防万一其他人偶然发现这个问题而回答


要显示
fragment\u music
版面而不显示
content\u main
,您需要将
fragment\u music
版面的
相对版面的背景设置为
白色
(或您想要的任何内容)这样一来,
content\u main
将隐藏,而您的
fragment\u布局将是您希望看到的所有内容。

是否用于主活动?我的其他片段工作得很好,我只需要在调用那里Id时隐藏主要内容,怎么做?我不知道
main\u activity
content\u main
是什么。您的代码中没有提到它。我假设它是一个单独的片段,只需在片段转换中使用
replace
就可以替换它。我已经更新了我的问题,以便于理解。首先,更改
FragmentManager
fragment
的支持库实现,看看这是否解决了您的问题。如果没有,您应该创建一个以易于重新创建的方式清楚地显示您的问题。
<?xml version="1.0" encoding="utf-8"?>
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:id="@+id/imageView3"
    android:src="@drawable/jmholdmy"
    android:layout_gravity="center_horizontal|top" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="100dp"
    android:layout_height="74dp"
    android:id="@+id/pause"
    android:layout_alignTop="@+id/play"
    android:layout_toRightOf="@+id/play"
    android:layout_toEndOf="@+id/play"
    android:layout_gravity="center_horizontal|bottom"
    android:background="@drawable/pausebtnimg" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="113dp"
    android:layout_height="72dp"
    android:id="@+id/stop"
    android:layout_alignTop="@+id/play"
    android:layout_toRightOf="@+id/pause"
    android:layout_toEndOf="@+id/pause"
    android:layout_gravity="right|bottom"
    android:background="@drawable/stopbtnimg" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="114dp"
    android:layout_height="72dp"
    android:id="@+id/play"
    android:layout_marginTop="170dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_gravity="left|bottom"
    android:background="@drawable/playbtnimg" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="258dp"
    android:id="@+id/textView4"
    android:layout_gravity="left|bottom"
    android:text="Taking from our debut EP Hold my tongue was our first released song, it won play at the Picnic competition which brought Just Mikey to the stage at the largest festivil in Europe Electric Picnic, This is the first of many to come so enjoy.  " />

<TextView
    android:layout_width="314dp"
    android:layout_height="156dp"
    android:text="Buy Track: https://www.facebook.com/search/top/?q=just%20mikey%20google%20play"
    android:id="@+id/textView5"
    android:layout_gravity="center_horizontal|bottom"
    android:backgroundTint="#000000"
    android:backgroundTintMode="screen" />