2016-11-16 34 views
1

我试图更换主屏幕片段displaynews片段,但片段被加入,而不是在这里的代码替换如何使用在Android Studio中的另一个片段取代片段

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> 

mainacitivity.class

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

    try { 
     fragment = (Fragment) fragmentClass.newInstance(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    // Insert the fragment by replacing any existing fragment 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.home_fragment,fragment).commit(); 

displaynews.class

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> 

HomeScreen.class

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

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

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> 

回答

1
如果硬编码在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(); 

source // example - 仍然得到问题?你可能会发现一个解决方案here

+0

不,我不能得到解决方案 –

+0

@AjiteshSakaray因此,我不明白你的问题 – HAlexTM

+0

看到我的代码,我必须在这里命名为displaynews和homescreen.i有content_main.xml作为主要xml文件在这里我想添加displaynews.xml文件,当我点击一个按钮时,displaynews片段应该被替换为homescreen片段,我该如何做到这一点 –

相关问题