废话:在上一篇的博客中我们给出了Fragment的简单介绍,这一片博客给大家介绍一下Fragment到底该怎样用。主要都用在哪方面等等。

需求:现有一个界面,要求,竖屏时界面的背景颜色为红色,横屏时界面的的背景颜色为黄色。(主要目的是为了给大家演示一下Fragment实现动态UI效果)

直接看代码好了:

一、背景颜色为红色的Fragment

  1. package com.yw.myapiupdate.fragment;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Fragment;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9.  
  10. import com.yw.myapiupdate.R;
  11.  
  12. @SuppressLint("NewApi")
  13. public class FragmentRed extends Fragment{
  14.  
  15. @Override
  16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  17. Bundle savedInstanceState) {
  18. return inflater.inflate(R.layout.fragmentred, container,false);
  19. }
  20. }

红色布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6.  
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:background="#ff0000"
  11. android:text="这是一个fragment的测试程序"/>
  12. </LinearLayout>

二、背景颜色为黄色的Fragment

  1. package com.yw.myapiupdate.fragment;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Fragment;
  5. import android.os.Bundle;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9.  
  10. import com.yw.myapiupdate.R;
  11.  
  12. @SuppressLint("NewApi")
  13. public class FragmentYellow extends Fragment{
  14.  
  15. @Override
  16. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  17. Bundle savedInstanceState) {
  18. return inflater.inflate(R.layout.fragmentyellow, container,false);
  19. }
  20. }

黄色布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:text="这是一个fragment的测试程序"
  10. android:background="#ffff00"/>
  11.  
  12. </LinearLayout>

三、承载这两个布局的Activity

  1. package com.yw.myapiupdate.fragment;
  2.  
  3. import android.annotation.SuppressLint;
  4. import android.app.Activity;
  5. import android.os.Bundle;
  6. import android.view.Display;
  7.  
  8. import com.yw.myapiupdate.R;
  9.  
  10. @SuppressLint("NewApi")
  11. public class MainActivity extends Activity{
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.fragment_layout);
  16. Display display = getWindowManager().getDefaultDisplay();
  17. if(display.getWidth() > display.getHeight()){
  18. FragmentRed red = new FragmentRed();
  19. getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, red).commit();
  20. }else{
  21. FragmentYellow yellow = new FragmentYellow();
  22. getFragmentManager().beginTransaction().replace(R.id.fragment_linear_layout, yellow).commit();
  23. }
  24. }
  25. }

主文件布局:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/fragment_linear_layout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="horizontal"
  7. android:baselineAligned="false">
  8. </LinearLayout>

Android Fragment(二)的更多相关文章

  1. Android Fragment (二) 实例2

    由于看客的要求,我就把读者所要的写出来. 由于上一篇是每一个Fragment 实例了同一个layout.xml ,造成了读者的困惑,这篇我就让每一个Fragment 加载一个不同的layout.xml ...

  2. Android Fragment (二) 实例1

    千呼万唤始出来,今天就也写一篇Frament 的简单实例.先看效果: 一看这效果,首先我们的配置资源文件:new -->android xml -->selector --> 四个图 ...

  3. Android fragment (二)

    怎样使用fragment? 1.首先你要确定下你有多少个fragment要使用在一个activity里. 2.依据你的fragment的数量,创建继承自fragment的class.然后依据实际需求重 ...

  4. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

  5. Android Fragment使用(四) Toolbar使用及Fragment中的Toolbar处理

    Toolbar作为ActionBar使用介绍 本文介绍了在Android中将Toolbar作为ActionBar使用的方法. 并且介绍了在Fragment和嵌套Fragment中使用Toolbar作为 ...

  6. Android Fragment使用(三) Activity, Fragment, WebView的状态保存和恢复

    Android中的状态保存和恢复 Android中的状态保存和恢复, 包括Activity和Fragment以及其中View的状态处理. Activity的状态除了其中的View和Fragment的状 ...

  7. Android Fragment使用(一) 基础篇 温故知新

    Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...

  8. Android Fragment详解

    一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...

  9. Android Fragment 简单实例

    Android上的界面展示都是通过Activity实现的.Activity实在是太经常使用了.我相信大家都已经很熟悉了,这里就不再赘述. 可是Activity也有它的局限性,相同的界面在手机上显示可能 ...

  10. android fragment传递参数_fragment之间传值的两种方法

    在Activity中加载Fragment的时候.有时候要使用多个Fragment切换.并传值到另外一个Fragment.也就是说两个Fragment之间进行参数的传递.查了很多资料.找到两种方法.一种 ...

随机推荐

  1. 集合框架四(Map)

    Map的主要实现类: --HashMap:Map的主要实现类(掌握) --LinkedHashMap:使用链表维护添加进Map中的顺序,遍历时按添加时的顺序遍历 --TreeMap:按照添加进Map中 ...

  2. 用node.js模拟服务器和客户端

    服务器 代码 var net = require("net") var server = net.createServer(); server.listen(12306," ...

  3. eclipse 中springboot2.0整合jsp 出现No Java compiler available for configuration options compilerClassName

    今天使用eclipse创建springboot整合jsp出现一个问题,在idea中并没有遇到这个问题.最后发现是需要在eclipse中添加一个eclipse依赖,依赖如下: <dependenc ...

  4. IIS 下调用证书出现异常解决方案 (C#)

    程序发布前,跑在vs上是没问题的,当发布后,程序就报错了.通过系统日志找到了错误所在:证书调用时出现了异常.原因是:在IIS上调用证书是需要配置的,具体配置如下: 一. 确保证书已安装 1. 点击 [ ...

  5. CSS 小结笔记之滑动门技术

    所谓的滑动门技术,就是指盒子背景能够自动拉伸以适应不同长度的文本.即当文字增多时,背景看起来也会变长. 大多数应用于导航栏之中,如微信导航栏: 具体实现方法如下: 1.首先每一块文本内容是由a标签与s ...

  6. APP性能测试指标和测试方法

    流量 常用方法 方法一:Android系统自带统计功能(总体流量数值) Proc/uid_stat/{UID}/tcp_snd和tcp_rcv UID是每个app安装时候分配的唯一编号用于识别该app ...

  7. LeeTCode题解之Remove Duplicates from Sorted List

    1.题目描述 2.问题分析 对于链表中的每一个元素,找到其后面和它不相等的第一个元素,然后指向该元素. 3.代码 ListNode* deleteDuplicates(ListNode* head) ...

  8. python数据类型之间的转换

    1,字符串转整型,前提条件是该字符串为纯数字. a = '1' a = int(a) 2,整型转字符串 a= 1 a = str(a) 3,整型转浮点型 a = 1 a = float(a) 4,浮点 ...

  9. python终端总是无法删除字符

    yum install readline-devel

  10. Azure 托管镜像和非托管镜像对比

    目前中国区 Azure 也已经可以使用命令制作托管镜像了.但对于托管镜像和非托管镜像,就像托管磁盘和非托管磁盘一样,很多人可能一开始无法理解.这里就此进行了一个简单对比: 通过对比测试,这里总结了这两 ...