有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

这是我的主要活动

package com.justmikey.justmik;

 import 安卓.media.MediaPlayer;
 import 安卓.os.Bundle;
 import 安卓.support.design.widget.FloatingActionButton;
 import 安卓.support.design.widget.Snackbar;
 import 安卓.support.v4.app.FragmentManager;
 import 安卓.support.v7.internal.widget.ButtonBarLayout;
 import 安卓.text.method.ScrollingMovementMethod;
 import 安卓.view.View;
 import 安卓.support.design.widget.NavigationView;
 import 安卓.support.v4.view.GravityCompat;
 import 安卓.support.v4.widget.DrawerLayout;
 import 安卓.support.v7.app.ActionBarDrawerToggle;
 import 安卓.support.v7.app.AppCompatActivity;
 import 安卓.support.v7.widget.Toolbar;
 import 安卓.view.Menu;
 import 安卓.view.MenuItem;
 import 安卓.widget.Button;
 import 安卓.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) {
    安卓.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
    安卓:layout_width="fill_parent"
    安卓:layout_height="fill_parent"
    安卓:id="@+id/content_frame">

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

    <TextView
        安卓:layout_width="fill_parent"
        安卓:layout_height="274dp"
        安卓: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 "
        安卓:id="@+id/scrollMain"
        安卓:layout_gravity="center_horizontal|bottom"
        安卓:layout_alignBottom="@+id/scrollView"
        安卓:layout_alignParentLeft="true"
        安卓:layout_alignParentStart="true"
        安卓:layout_marginBottom="28dp"
        安卓:layout_alignParentRight="true"
        安卓:layout_alignParentEnd="true"
        安卓:background="#ffffff"
        安卓:padding="10dp" />

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


</FrameLayout>

</RelativeLayout>

这是我的音乐碎片

package JustMikey.Fragments;

import 安卓.app.Fragment;
import 安卓.graphics.LightingColorFilter;
import 安卓.graphics.PorterDuff;
import 安卓.media.MediaPlayer;
import 安卓.os.Bundle;
import 安卓.support.annotation.Nullable;
import 安卓.view.LayoutInflater;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;

import com.justmikey.justmik.R;

 /**
  *     Created by James on 24/02/2016.
  */
public class MusicFragment extends Fragment implements View.OnClickListener {

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




@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle  savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragmentmusic, container,  false);


    //call my button views in by using the ID tag
    play = (Button) rootView.findViewById(R.id.play);
    pause = (Button) rootView.findViewById(R.id.pause);
    stop = (Button) rootView.findViewById(R.id.stop);

    //set an onClickListener to make my buttons clickable
    play.setOnClickListener(this);
    pause.setOnClickListener(this);
    stop.setOnClickListener(this);

    //Change the colour of the pause button for a nice design effect
    pause.getBackground().setColorFilter(new LightingColorFilter(0x00000000,  0xFFAA0000));
    //return the view
    return rootView;
}
// onClick Method for the play, pause and stop buttons
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.play : if(mp == null) {
            mp = MediaPlayer.create(getActivity(), R.raw.holdmytong);
        }
            mp.start();
            break;
        case R.id.pause : mp.pause();break;

        case R.id.stop : mp.stop();mp = null;break;
    }

    }



}

音乐版式

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

<ImageView
    安卓:layout_width="fill_parent"
    安卓:layout_height="wrap_content"
    安卓:adjustViewBounds="true"
    安卓:scaleType="fitCenter"
    安卓:id="@+id/imageView3"
    安卓:src="@drawable/jmholdmy"
    安卓:layout_gravity="center_horizontal|top" />

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

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

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

<TextView
    安卓:layout_width="match_parent"
    安卓:layout_height="258dp"
    安卓:id="@+id/textView4"
    安卓:layout_gravity="left|bottom"
    安卓: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
    安卓:layout_width="314dp"
    安卓:layout_height="156dp"
    安卓:text="Buy Track: https://www.facebook.com/search/top/?q=just%20mikey%20google%20play"
    安卓:id="@+id/textView5"
    安卓:layout_gravity="center_horizontal|bottom"
    安卓:backgroundTint="#000000"
    安卓:backgroundTintMode="screen" />


共 (2) 个答案

  1. # 1 楼答案

    AppCompatActivity应该使用通过getSupportFragmentManager()检索的FragmentManager

  2. # 2 楼答案

    现在回答这个问题已经太晚了,但我只是想回答一下,以防有人无意中发现这个问题

    要显示fragment_music版面而不在其后面显示content_main,您需要将fragment_music版面的Relative Layout背景设置为white(或任何您想要的),这样content_main将隐藏,您的fragment_layout将是您想要看到的所有内容