Android 基础知识

Android 主页 Android 概述 Android 下载安装和开发环境配置 Android Studio IDE Android 架构 Android 应用程序组件 Android Hello World 示例 Android 资源 Android 活动 Android 服务 Android 广播接收器 Android 内容提供者 Android 片段 Android Intents/Filters

Android 用户界面

Android UI 布局 Android UI 控件 Android 事件处理 Android 样式和主题 Android 自定义组件

Android 高级概念

Android 拖放 Android 通知 Android 基于位置的服务 Android 发送电子邮件 Android 发送短信 Android 拨打电话 Android 发布应用程序

Android 实用示例

Android 警报对话框 Android 动画 Android 音频捕捉 Android 音频管理器 Android 自动完成 Android 最佳实践 Android 蓝牙 Android 相机 Android 剪贴板 Android 自定义字体 Android 数据备份 Android 开发者工具 Android 模拟器 Android Facebook 集成 Android 手势 Android 谷歌地图 Android 图像效果 Android 图像切换 Android 内部存储 Android JetPlayer Android JSON 解析器 Android Linkedin 集成 Android 旋转加载器 Android 本地化 Android 登录应用 Android 媒体播放器 Android 多点触控 Android 导航 Android 网络连接 Android NFC 指南 Android PHP/MySQL Android 进度圈 Android 进度条 Android 推送通知 Android 渲染脚本 Android RSS 阅读器 Android 屏幕投射 Android SDK 管理器 Android 传感器 Android 会话管理 Android 共享首选项 Android SIP 协议 Android 拼写检查器 Android SQLite 数据库 Android 支持库 Android 测试 Android 文字转语音 Android TextureView Android Twitter 集成 Android UI 设计 Android UI 模式 Android UI 测试 Android WebView 布局 Android Wi-Fi Android Widgets Android XML 解析器

Android 其他

Android 有用的资源 Android 测验


Android - 单帧片段

单帧片段

单帧片段专为手持设备(手机)等小屏幕设备设计,应在android 3.0以上版本。


示例

这个例子将向您解释如何创建自己的Fragments。在这里,我们将创建两个片段,其中一个将在设备处于横向模式时使用,另一个片段将用于纵向模式。 因此,让我们按照以下步骤类似于我们在创建 Hello World 示例 时所遵循的步骤 −

步骤 描述
1 您将使用 Android StudioIDE 创建一个 Android 应用程序,并将其命名为 MyFragments,位于包 com.example.myfragments 下,Activity 为空白。
2 修改主活动文件MainActivity.java,如下代码所示。 在这里,我们将检查设备的方向,因此我们将在不同的片段之间切换。
3 在包 com.example.myfragments 下创建两个 java 文件 PM_Fragment.java 和 LM_Fragement.java 来定义您的片段和相关方法。
4 创建布局文件 res/layout/lm_fragment.xml 和 res/layout/pm_fragment.xml 并为这两个片段定义布局。
5 修改 res/layout/activity_main.xml 文件的默认内容以包含两个片段。
6 在 res/values/strings.xml 文件中定义所需的常量
7 运行应用程序以启动 Android 模拟器并验证应用程序中所做更改的结果。

以下是修改后的主活动文件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();
   }

}

创建两个片段文件 LM_Fragement.java 和 PM_Fragment.java

以下是 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 TutorialsPoint7 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 TutorialsPoint7 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 文件的内容,其中包括您的片段 −

<?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 运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的运行图标。 Android Studio 会在您的 AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示模拟器窗口,您将在其中单击菜单按钮以查看以下窗口。请耐心等待,因为根据您的计算机速度可能需要一些时间 −

Android 纵向片段演示

要更改模拟器屏幕的模式,让我们执行以下操作 −

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

  • ctrl+F11 在 Windows 上。

  • ctrl+F11 在 Linux 上。

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

Android 横向片段演示

这样,您可以通过不同的片段使用相同的活动但不同的 GUI。 您可以根据您的要求为不同的 GUI 使用不同类型的 GUI 组件。

❮ Android 片段



关于我们 | 隐私条款 | 学习路线
返回顶部