Android 中的 Single frame fragments

首页 / Android入门教程 / Android 中的 Single frame fragments

Single frame fragment是为小屏幕设备(如手持设备)设计的,并且应高于android 3.0版本。

示例

本示例将向您说明如何创建自己的fragment。在这里,我们将创建两个fragment,其中一个fragment将在设备处于横向模式时使用,另一个fragment将在纵向模式下使用。

无涯教程网

以下是修改后的主要Activity文件 MainActivity.java 的内容-

package com.example.myfragments;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.res.Configuration;
import android.os.Bundle;

public class MainActivity extends Activity {

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Configuration config = getResources().getConfiguration();

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

      /**
         * Check the device orientation and act accordingly
      */
		
      if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
         /**
            * Landscape mode of the device
         */
         LM_Fragement ls_fragment = new LM_Fragement();
         fragmentTransaction.replace(android.R.id.content, ls_fragment);
      }else{
         /**
            * Portrait mode of the device
         */
         PM_Fragement pm_fragment = new PM_Fragement();
         fragmentTransaction.replace(android.R.id.content, pm_fragment);
      }
      fragmentTransaction.commit();
   }

}

创建两个fragment文件 LM_Fragement.java 和 PM_Fragment.java

链接:https://www.learnfk.comhttps://www.learnfk.com/android/android-single-fragments.html

来源:LearnFk无涯教程网

以下是 LM_Fragement.java 文件的内容-

package com.example.myfragments;

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

/**
   * Created by LearnFk7 on 8/23/2016.
*/

public class LM_Fragement extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      /**
         * Inflate the layout for this fragment
      */
      return inflater.inflate(R.layout.lm_fragment, container, false);
   }
}

以下是 PM_Fragement.java 文件的内容-

package com.example.myfragments;

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

/**
   * Created by LearnFk7 on 8/23/2016.
*/

public class PM_Fragement extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      /**
         * Inflate the layout for this fragment
      */
      return inflater.inflate(R.layout.pm_fragment, container, false);
   }
}

在 res/layout 目录下创建两个布局文件 lm_fragement.xml 和 pm_fragment.xml ,以下是 lm_fragement.xml 文件的内容-

<?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="20px" />

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

</LinearLayout>

以下是 pm_fragment.xml 文件的内容-

<?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="20px" />

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

</LinearLayout>

以下是 res/layout/activity_main.xml 文件的内容,其中包括您的fragment-

<?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:name="com.example.fragments"
      android:id="@+id/lm_fragment"
      android:layout_weight="1"
      android:layout_width="0dp"
      android:layout_height="match_parent" />
   
   <fragment
      android:name="com.example.fragments"
      android:id="@+id/pm_fragment"
      android:layout_weight="2"
      android:layout_width="0dp"
      android:layout_height="match_parent" />

</LinearLayout>

确保您具有 res/values/strings.xml 文件的以下内容-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">My Application</string>
   <string name="landscape_message">This is Landscape mode fragment</string>
   <string name="portrait_message">This is Portrait mode fragment></string>
</resources>

让我们尝试运行刚刚创建的修改后的 MyFragments 应用程序,我假设您在进行环境设置时创建了 AVD 。要从Android Studio运行该应用,请打开您项目的Activity文件之一,然后从工具栏中单击"运行"图标。 Android Studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将显示"Emulator"窗口,您将在其中单击"菜单"按钮以查看以下窗口。请耐心等待,因为这可能要花一些时间,具体取决于您的计算机速度-

Android Portrait Fragment Demo

要更改仿真器屏幕的模式,请执行以下操作-

    在Mac上
  • fn + control + F11 可以将横向更改为纵向,反之亦然。

  • 在Windows上是
  • ctrl + F11 。

  • 在Linux上是
  • ctrl + F11 。

更改模式后,您将能够看到为横向模式实现的GUI,如下所示-

Android Landscape Fragment Demo

这样,您可以通过不同的fragment使用相同的Activity,但使用不同的GUI。您可以根据需要将不同类型的GUI组件用于不同的GUI。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Service Mesh实践指南 -〔周晶〕

推荐系统三十六式 -〔刑无刀〕

深入浅出区块链 -〔陈浩〕

技术领导力实战笔记 -〔TGO鲲鹏会〕

如何设计一个秒杀系统 -〔许令波〕

安全攻防技能30讲 -〔何为舟〕

物联网开发实战 -〔郭朝斌〕

Kubernetes入门实战课 -〔罗剑锋〕

AI大模型之美 -〔徐文浩〕

好记忆不如烂笔头。留下您的足迹吧 :)