3.15. Android single frame fragment

发布时间 : 2025-10-25 13:33:23 UTC      

Page Views: 9 views

单帧碎片:单帧碎片是为小屏幕设备所设计的,如手持设备(移动电话),Android 3.0 以上版本支持。

3.15.1. Example

This example explains how to create your own fragments. Here we create two fragments, one of which is used when the device is landscape and the other is used when the device is vertical. Let’s start with the steps.

Steps

Description

1

Use Android Studio IDE to create an Android application named Single Fragments and package name cn.uprogrammer.singlefragments.

2

Modify the main active file MainActivity.java as shown below. Here we will check the direction of the device and switch different fragments based on it.

3

Create PortraitFragment.java and LandscapeFragment.java files under the cn.uprogrammer.singlefragments package and associate the methods.

4

Create the layout files res/layout/landscape_fragment.xml and res/layout/portrait_fragment.xml to define the layout of the two fragments.

5

Modify the res/layout/activity_main.xml to contain 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 application’s changes.

下面是主要活动文件 src/cn.uprogrammer.singlefragments/MainActivity.java 的内容:

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(); /** * 检测设备方向,并做相应地操作。 */ if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { /** * 设备的横屏模式。 */ LandscapeFragment ls_fragment = new LandscapeFragment(); fragmentTransaction.replace(android.R.id.content, ls_fragment); }else{ /** * 设备的竖屏模式。 */ PortraitFragment pm_fragment = new PortraitFragment(); fragmentTransaction.replace(android.R.id.content, pm_fragment); } fragmentTransaction.commit(); } } 

在包 cn.uprogrammer.singlefragments 下创建两个碎片文件 LandscapeFragment.java PortraitFragment.java

以下是 LandscapeFragment.java 文件的内容:

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); } } 

以下是 PortraitFragment.java 文件的内容:

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); } } 

在目录 res/layout 目录下创建2个布局文件 landscape_fragment.xml portrait_fragment.xml

以下是 landscape_fragment.xml 文件的内容:

        

以下是 portrait_fragment.xml 文件的内容:

        

以下是 res/layout/activity_main.xml 文件的内容,其中包含两个碎片:

        

确保 res/values/strings.xml 文件包含如下内容:

  Single Fragment Hello world! Settings 这是横屏模式碎片 这是竖屏模式碎片  

Let’s run the Single Fragments application that we just modified. I assume that you have created AVD when you installed the environment. Open the active file in your project and click the icon in the toolbar to run the application in Android Studio. Android Studio installs the application on AVD and starts it. If all goes well, it will be displayed on the simulator window as follows:

image33

Change the orientation mode of the simulator screen as follows:

  • Fn+control+F11 changes horizontal screen to vertical screen on mac, and vice versa

  • Ctrl+F11 is on windows.

  • Ctrl+F11 is on Linux.

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

image34

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

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。