habe ich versucht, durch Zugabe eines Fragments (AnotherFragment) zu einem Layout (sample_content_fragment) in mainactivity ein Fragment dynamisch laden .Aber es nicht funktioniert, wenn ich button1.The Code unten haben so den Fehler ausgeführt klicken ist nicht der Button.Fragment In Arbeit nicht
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
AnotherFragment fragment = new AnotherFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.sample_content_fragment, fragment);
transaction.commit();
break;
}
Und hier können Sie mainactivity Layout sehen:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/fregment_test"
android:name="com.example.henucmapus.TestFragment"
android:layout_width="match_parent"
android:layout_height="112dp" />
<FrameLayout
android:id="@+id/sample_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Hier ist das Fragment ich möchte laden. Hier
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AnotherFragment extends Fragment{
public View OnCreateView(LayoutInflater inflater,ViewGroup container,
Bundle savedInstanceState){
View view =inflater.inflate(R.layout.anotherfragment,container, false);
return view;
}
}
ist das Fragment-Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11111111111"
/>
</LinearLayout>
sowieso getan Sieht aus wie Sie versuchen, den Inhalt in der anfänglichen zu ersetzen Fragment anstelle des Fragments selbst. – RScottCarson