Android – how do I create multiple clips programmatically?

I try to programmatically display multiple clips on one screen. I can do this by including each clip in the active layout file. However, I am confused when I try to operate programmatically. These are the two clips on the screen so far

Main category:

public class MainActivity extends FragmentActivity {

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

        FragmentOne one = new FragmentOne();
        FragmentTwo two = new FragmentTwo();
        FragmentThree three = new FragmentThree();

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.imOne, one, "fragmentone");
        ft.add(R.id.imTwo, two, "fragmenttwo");
        ft.add(R.id.imThree, three, "fragmenthree");
        ft.commit();

    }
}

Segment 1:

public class FragmentOne extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_one, container, false);
        return view;
    }

}

Segment 2:

public class FragmentTwo extends Fragment{

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.frag_two, container, false);
        return view;
    }

}

The layout file of my two fragment classes only contains a simple textview. Then is the activity of my main class_ Main.xml layout file:

    <?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="horizontal" >

    <FrameLayout
        android:id="@+id/imOne"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imTwo"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/imThree"
        android:name="com.david.twofragexample.FragmentThree"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

When I try to do this programmatically, I_ I'm confused about what should be included in the main.xml file. Therefore, from the Android Developer's Guide( http://developer.android.com/guide/components/fragments.html )At first, it said to use

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FragmentOne fragment = new FragmentOne();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

Therefore, when I add this code to my oncreate method, I must update my activity_ What changes can I make to the main.xml file to do this programmatically? I didn't see r.id.fragment_ Where does the container come from? How can I determine the location of my new clip on the screen? thank you

Editor: I developed this on a tablet rather than a mobile phone. This has been solved. The above code will add three fragments programmatically

resolvent:

<?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="horizontal" >

    <FrameLayout
        android:id="@+id/list"
        android:name="com.david.twofragexample.FragmentOne"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

    <FrameLayout
        android:id="@+id/viewer"
        android:name="com.david.twofragexample.FragmentTwo"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />

</LinearLayout>

In your event:

FragmentOne one = new FragmentOne();
FragmentTwo two = new FragmentTwo();

fm = getSupportFragmentManager();
fm.beginTransaction().replace(R.id.list, one, "fragmentone").commit();
fm.beginTransaction().replace(R.id.viewer, two, "fragmenttwo").commit();

I suggest you study:

http://developer.android.com/reference/android/app/Fragment.html

And fully understand the fragment life cycle, etc. the way I show is fast and dirty, but not the best neccesary

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>