Оценок пока нет Add Fragment as content to Activity in Android

Today we try add Fragment layout in another Activity layout, that reduce  complexity develop user interface. For this example we need 3 layout: main.xml, fragment1.xml, fragment2.xml, and 3 classes: MainActivity.java, Fragment1.java, Fragment2.java

Code of xml layouts

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"    >
</LinearLayout>
<?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/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="I m fragment ONE"
        android:gravity="center"
        android:background="#5eff6a"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
<?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/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="I m fragment ONE"
        android:gravity="center"
        android:background="#5eff6a"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

 

Code of Java — classes

package com.example.dynamicfragments;

import android.support.v7.app.ActionBarActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;

public class MainActivity extends ActionBarActivity {

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

		FragmentManager fm = getFragmentManager();
		FragmentTransaction fragmentTransaction = fm.beginTransaction();

		// get the display mode
		int displaymode = getResources().getConfiguration().orientation;
		if (displaymode == 1) { // it portrait mode
			Fragment1 f1 = new Fragment1();
			fragmentTransaction.replace(android.R.id.content, f1);
		} else {// its landscape
			Fragment2 f2 = new Fragment2();
			fragmentTransaction.replace(android.R.id.content, f2);
		}
		fragmentTransaction.commit();

	}
}

 

package com.example.dynamicfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment1 extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup vg,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment1, vg, false);
	}
}

 

package com.example.dynamicfragments;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
	public View onCreateView(LayoutInflater inflater, ViewGroup vg,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment2, vg, false);
	}
}

 

Conclusion result for this example

Пожалуйста, оцените материал

WebSofter

Web - технологии