Android 单帧碎片

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

实例

该实例解释如何创建自己的碎片。这里我们创建两个碎片,其中一个被使用在设备是横屏的时候,另一个被使用在设备是竖屏的时候。下面让我们按照步骤开始吧。

步骤

描述

1

使用 Android Studio IDE 来创建一个 Android 应用程序,命名为 Single Fragments,包名 cn.uprogrammer.singlefragments。

2

修改如下所示的主活动文件 MainActivity.java。这里我们将要检查设备的方向,并基于此切换不同的碎片。

3

在 cn.uprogrammer.singlefragments 包下创建 PortraitFragment.java 和 LandscapeFragment.java 两个文件,并关联方法。

4

创建布局文件 res/layout/landscape_fragment.xml 和 res/layout/portrait_fragment.xml 来定义两个碎片的布局。

5

修改 res/layout/activity_main.xml 来包含两个碎片。

6

在 res/values/strings.xml 中定义需要的常量。

7

启动Android模拟器来运行应用程序,并验证应用程序所做改变的结果。

下面是主要活动文件 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 文件的内容:

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">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/landscape_message"

android:textColor="#000000"

android:textSize="28sp" />

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

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">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/portrait_message"

android:textColor="#000000"

android:textSize="28sp" />

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

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal">

android:id="@+id/landscape_fragment"

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="match_parent" />

android:id="@+id/portrait_fragment"

android:layout_weight="2"

android:layout_width="0dp"

android:layout_height="match_parent" />

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

Single Fragment

Hello world!

Settings

这是横屏模式碎片

这是竖屏模式碎片

让我们运行刚刚修改的 Single Fragments 应用程序。我假设你已经在安装环境时创建了 AVD。打开你的项目中的活动文件,点击工具栏中的

图标来在 Android Studio 中运行应用程序。Android Studio 在 AVD 上安装应用程序并启动它。如果一切顺利,将在模拟器窗口上显示如下:

按照下列操作来改变模拟器屏幕的方向模式:

fn+control+F11 在mac上改变横屏为竖屏,反之亦然

ctrl+F11 在windows上

ctrl+F11 在Linux上

当你修改模式,你将看到适用于横屏模式的页面实现:

通过这种方法,你可以在同一个活动中通过使用不用的碎片来实现不同的界面。可以按照你的需求使用不同类型的界面组件来构建界面。

android 首页6个碎片,Android 单帧碎片相关推荐

  1. Android技术馆系列之:Android 碎片的介绍

    借用海子的一句诗来抒发一下感慨"每天喂马砍柴,面朝大海,春暖花开" Android 碎片(Fragment) 碎片是活动的一部分,是的活动更加的模块化设计.我们可以认为碎片是一种子 ...

  2. android从活动返回碎片,Android碎片

    甲 片段 是一块活动这使更多的模块化活动设计的.如果我们说一个片段是一种 子活动 就没有错. 以下是片段的重点 - 片段具有自己的布局和自己的行为以及自己的生命周期回调. 您可以在活动运行时在活动中添 ...

  3. android 获取视频大小,Android 获取视频缩略图(获取视频每帧数据)的优化方案

    速度对比 左边的图片是通过方式1 右边的图片是通过方式2 speed.gif 速度优化,效果拔群. 在缩小2倍的Bitmap输出情况下 使用MediaMetadataRetriever 抽帧的速度,每 ...

  4. Android FFmpeg移植总攻略——获取视频帧数(亲测可用)

    第一次尝试使用Android 移植FFmpeg算法,一路坎坷,最终做如下总结,适用于Android手机.Android开发板.亲测可用. 一.下载组件 在Android Studio中下载所需组件:C ...

  5. Android中实现一个简单的逐帧动画(附代码下载)

    场景 Android中的逐帧动画,就是由连续的一张张照片组成的动画. 效果 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...

  6. android 自定义表单,Android实现Ant Design 自定义表单组件

    Ant Design 组件提供了Input,InputNumber,Radio,Select,uplod等表单组件,但实际开发中这是不能满足需求,同时我们希望可以继续使用Form提供的验证和提示等方法 ...

  7. Android中的动画2(逐帧动画)

    逐帧动画就比较简单了,就是一帧一帧的播放动画,每一帧都是有我们来定义的. 在res/drawable文件夹下新建一个Root element为animation-list的xml文件,命名为anima ...

  8. Android 收银机Wifi 连接厨房厨单打印机

    Android 收银机Wifi 连接厨房厨单打印机 说明 第一次集成热敏打印机,对此相关知识为零,以快速接入为目的. 这里主要记录说明在集成过程中遇到的问题以及排查解决的办法.完整可用Android ...

  9. QML调用Android摄像头,并将每一帧传入C++进行处理(QML 对象,转化为C++对象)

    第一步:搭建好Qt for Android 开发环境 搭建环境参考:Qt for Android 环境搭建 第二步,创建Qt工程 注意选择创建Qt Quick工程 .pro文件中加入模块: QT += ...

最新文章

  1. 使用UEFI模式安装win10中的格式化磁盘问题
  2. Java concurrent 学习
  3. citespace与mysql_科学网—如何用CiteSpace整理数据 - 陈超美的博文
  4. DataTable添加列和行的三种方法
  5. python爬虫表格中清除空格_爬虫清洗:python strip()函数 去空格\n\r\t函数的用法
  6. CVPR 2017 ADNet:《 Action-Decision Networks for Visual Tracking with Deep Reinforcement Learning》论文笔记
  7. NGUI减少Drawcall
  8. 制造领域的人工智能技术
  9. C/C++程序员必读的十本书(上)
  10. 诗与远方:无题(八十)- 吸烟而作
  11. react滑动切换tab动画效果_后端设计中,如何用axure实现table切换动效?
  12. VS2017的C++开发心得(一)VS的项目创建
  13. vue: table制作发货单表格并打印
  14. 了解uni-app只需这一篇就足够了
  15. angular写的移动端模板《二》
  16. 电子密码锁的设计(Verilog HDL实现)
  17. 一个毕业生的年终总结
  18. 计算机十六进制是什么意思,2进制和16进制是什么意思?它们之间如何换算?
  19. 解决谷歌浏览器最新chrome9+ 版本CORS跨域问题
  20. Java-AQI计算

热门文章

  1. PHP的uniqid
  2. python条件表达式连起来写一段话_python学习笔记十三条件表达式应用
  3. 上顿号符号_上顿号符号_标点符号(1):谈谈顿号的用法
  4. 微型计算机原理综合实验,微机原理综合实验指导书
  5. axios封装_VUE.JS请求工具Axios的封装
  6. webstorm 两个文件对比不同_DOS 入门到精通 使用 fc 命令比较两个文件,并逐一显示不同之处...
  7. 【uni-app】在新窗口中打开链接
  8. mysql 5.6 缓存_为什么默认情况下从MySQL 5.6开始禁用query_cache_type?
  9. Linux753权限,linux的chmod与chown命令详解
  10. html字符串使用xpath,使用XPath和regex在HTML注释中提取文本