Runoob Tutorial ArchiveEnglish

Android single-frame fragment

Android single-frame fragment

Single-frame fragment:The single-frame fragment is designed for small screen devices, such as handheld devices (mobile phones), and is supported in Android 3.0 and above.


Example

This example explains how to create your own fragment. Here we create two fragments, one used when the device is in landscape mode and another used when the device is in portrait mode. Let's start with the steps below.

Steps Description
1 Use Android Studio IDE to create an Android application named Single Fragments, with package name cn.uprogrammer.singlefragments.
2 Modify the MainActivity.java main activity file as shown below. Here we will check the device orientation and switch different fragments based on it.
3 Create two files, PortraitFragment.java and LandscapeFragment.java, under the cn.uprogrammer.singlefragments package and associate the methods.
4 Create layout files res/layout/landscape_fragment.xml and res/layout/portrait_fragment.xml to define the layouts of the two fragments.
5 Modify res/layout/activity_main.xml to include two fragments.
6 Define the required constants in res/values/strings.xml.
7 Start the Android emulator to run the application and verify the results of the changes made to the application.

The content of the main activity file src/cn.uprogrammer.singlefragments/MainActivity.java is as follows:

Original prompt (Chinese, left untranslated)

package cn.uprogrammer.singlefragment;



import android.os.Bundle;

import android.app.Activity;

import android.app.FragmentManager;

import android.app.FragmentTransaction;

import android.content.res.Configuration;



public class MainActivity extends Activity {



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);



        Configuration config = getResources().getConfiguration();



        FragmentManager fragmentManager = getFragmentManager();

        FragmentTransaction fragmentTransaction =

                fragmentManager.beginTransaction();



        /**

         * Detect device orientation and perform corresponding operations.

         */

        if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {

            /**

             * Device landscape mode.

             */

            LandscapeFragment ls_fragment = new LandscapeFragment();

            fragmentTransaction.replace(android.R.id.content, ls_fragment);

        }else{

            /**

             * Device portrait mode.

             */

            PortraitFragment pm_fragment = new PortraitFragment();

            fragmentTransaction.replace(android.R.id.content, pm_fragment);

        }

        fragmentTransaction.commit();

    }

}

Create two fragment files, LandscapeFragment.java and PortraitFragment.java, in the package cn.uprogrammer.singlefragments.

The content of LandscapeFragment.java is as follows:

package cn.uprogrammer.singlefragment;



import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;





public class LandscapeFragment extends Fragment {



    @Override

    public View onCreateView(LayoutInflater inflater,

                             ViewGroup container, Bundle savedInstanceState) {

        /**

         * Inflate the layout for this fragment

         */

        return inflater.inflate(

                R.layout.landscape_fragment, container, false);

    }



}

The content of PortraitFragment.java is as follows:

package cn.uprogrammer.singlefragment;



import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;



public class PortraitFragment extends Fragment {



    @Override

    public View onCreateView(LayoutInflater inflater,

                             ViewGroup container, Bundle savedInstanceState) {

        /**

         * Inflate the layout for this fragment

         */

        return inflater.inflate(

                R.layout.portrait_fragment, container, false);

    }

}

Create two layout files, landscape_fragment.xml and portrait_fragment.xml, in the res/layout directory.

The content of landscape_fragment.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#7bae16">



    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/landscape_message"

        android:textColor="#000000"

        android:textSize="28sp" />



    <!-- More GUI components go here  -->



</LinearLayout>

The content of portrait_fragment.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="#666666">



    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/portrait_message"

        android:textColor="#000000"

        android:textSize="28sp" />



    <!-- More GUI components go here  -->



</LinearLayout>

The content of res/layout/activity_main.xml is as follows, which contains two fragments:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="horizontal">



    <fragment

        android:id="@+id/landscape_fragment"

        android:layout_weight="1"

        android:layout_width="0dp"

        android:layout_height="match_parent" />



    <fragment

        android:id="@+id/portrait_fragment"

        android:layout_weight="2"

        android:layout_width="0dp"

        android:layout_height="match_parent" />



</LinearLayout>

Ensure that the res/values/strings.xml file contains the following content:

Original prompt (Chinese, left untranslated)

<?xml version="1.0" encoding="utf-8"?>

<resources>



    <string name="app_name">Single Fragment</string>

    <string name="hello_world">Hello world!</string>

    <string name="action_settings">Settings</string>

    <string name="landscape_message">这是横屏模式碎片</string>

    <string name="portrait_message">这是竖屏模式碎片</string>

</resources>

Let's run the modified Single Fragments application. I assume you have already created an AVD during the installation environment setup. Open the activity file in your project and click the toolbarImageUse the icon to run the application in Android Studio. Android Studio installs and launches the application on the AVD. If everything goes well, it will be displayed in the emulator window as follows:

Image

Follow these steps to change the screen orientation mode of the emulator:

When you change the mode, you will see the page implementation for landscape mode:

Image

This way, you can achieve different interfaces in the same activity by using unused fragments. You can build the interface using different types of interface components according to your needs.