如何在android studio中用另一个片段替换片段

如何在android studio中用另一个片段替换片段,android,Android,我正试图用主屏幕片段替换displaynews片段,但在这里的代码中,该片段正在添加而不是替换 content_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" x

我正试图用主屏幕片段替换displaynews片段,但在这里的代码中,该片段正在添加而不是替换

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout 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/content_main"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ajitesh.newson.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <fragment
        android:id="@+id/home_fragment"
        android:name="com.ajitesh.newson.DisplayNews"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>
显示新闻类

 public class DisplayNews extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.displaynews, container, false);
    }
}
displaynews.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:id="@+id/display_news"
    android:layout_height="wrap_content">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="helllooooo"/>
</LinearLayout>
home_screen.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:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:text="this is home page"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"/>

</LinearLayout>

无法替换片段如果以xml硬编码,则应动态添加它以用另一个片段替换片段

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

//--仍然有问题吗?您可能会找到解决方案

不,我找不到solution@AjiteshSakaray所以我不明白你的问题看我的代码,我必须在这里创建名为displaynews和homescreen的片段。我将content_main.xml作为主xml文件,我要添加displaynews.xml文件,当我单击按钮时,displaynews片段应替换为homescreen片段如何执行此操作
    <?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:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/text1"
    android:text="this is home page"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"/>

</LinearLayout>
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();