Android Fragment 多标签应用
1、使用Fragment 可以方便的替代TabActivity、ViewGroup
2、同时也省去了在AndroidManifest.xml文件中 添加相应的Activity
3、Fragment 是3.0之后的功能,如果想向下兼容我们在导包时一定要注意了,该导入 import android.support.v4.app.FragmentActivity; 而不是 import android.app.Fragment;
4、同时向下兼容我们的Activity 要继承 FragmentActivity 而不是 Activity
5、核心代码:
package com.example.testdialog; import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView; @SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements OnClickListener {
private TextView tab1 = null;
private TextView tab2 = null;
private FragmentManager fm = null;
private FragmentTransaction ft = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
fm = getSupportFragmentManager(); } private void findViewById() {
this.tab1 = (TextView) this.findViewById(R.id.tab1);
this.tab2 = (TextView) this.findViewById(R.id.tab2);
this.tab1.setOnClickListener(this);
this.tab2.setOnClickListener(this);
} @Override
public void onClick(View v) { ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.tab1:
ft.replace(R.id.content, new MyFragment1());
break;
case R.id.tab2:
ft.replace(R.id.content, new MyFragment2());
break;
default:
break;
}
ft.commit();
} }
activity_main.xml 文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:id="@+id/tab1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Tab1" /> <TextView
android:id="@+id/tab2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Tab2" /> <TextView
android:id="@+id/tab3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Tab3" /> <TextView
android:id="@+id/tab4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Tab2" />
</LinearLayout> <LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip"
android:orientation="horizontal" >
</LinearLayout> </LinearLayout>
MyFragment1.java
package com.example.testdialog; import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button; @SuppressLint("NewApi")
public class MyFragment1 extends Fragment {
private Button button1 = null; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1, null);
button1 = (Button) view.findViewById(R.id.button1);
return view;
} }
6、这样我们就可以在相应的 Fragment 中做我们的UI设计了
Android Fragment 多标签应用的更多相关文章
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
- Android Fragment使用(一) 基础篇 温故知新
Fragment使用的基本知识点总结, 包括Fragment的添加, 参数传递和通信, 生命周期和各种操作. Fragment使用基础 Fragment添加 方法一: 布局里的标签 标识符: tag, ...
- Android Fragment应用实战
现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...
- Android Fragment
1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...
- Android Fragment 解析和使用
Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...
- Android Fragment详解
一.什么是Fragment Android在3.0中引入了fragments的概念,主要目的是用在大屏幕设备上--例如平板电脑上,支持更加动态和灵活的UI设计.平板电脑的屏幕要比手机的大得多,有更多的 ...
- Android Fragment 实例
Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍.Android Fragment使用.Android FragmentManage F ...
- 【转】基于Android Fragment功能的例子
原文网址:http://blog.csdn.net/eyu8874521/article/details/8252216 通过最近空闲时候对Fragment的学习,尝试着写了一个小Demo,将在开发的 ...
随机推荐
- phpcms V9 首页模板文件解析(转)
转自:http://www.cnblogs.com/Braveliu/p/5100018.html 转在了解了<phpcms V9 URL访问解析>之后,我们已经知道首页最终执行的是con ...
- C语言结构体中的函数指针
这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...
- STM32之外部中断控制
一.STM32外部中断 1.STM32外部中断结构图 如上图所示:主要包括四个环节,GPIO.AFIO.EXTI.NVIC.以STM32F103VE(100脚)为例说明硬件模块的数量: GPIO: ...
- Redis总录
设计 选择合适的数据对象来存储对象:String,List,Hash(Entity角色对象),Set,Zset(需要排序): 选择存储是全局的,还是局部的: 机制 批处理(pipeline) 事务(w ...
- iOS - 沙盒机制
iOS应用程序只能在为该程序创建的文件系统中读取文件,不可以去其他地方访问,此区域被称为沙盒.所有的非代码文件都要保存在此,例如图像,图标,声音,属性列表(plist文件),文本文件等.沙盒机制作为一 ...
- Java 判断操作系统类型(适用于各种操作系统)
Java 判断操作系统类型(适用于各种操作系统) 最近一段时间写一个授权的程序,需要获取很多信息来保证程序不能随意复制使用,必须经过授权才可以. 为了限制用户使用的操作系统,必须有统一的方法来获取才可 ...
- 15 个响应式的 jQuery 图像滑块插件
设计师和开发人员总是试图使用新技术让网站更智能,而我们发现在许多网站上 jQuery 的图像滑块插件是非常受欢迎的.本文继续介绍 15 个 jQuery 图像滑块插件以供您选择. ELASTISLID ...
- Mozilla公布WebVR API标准草案
随着信息技术的迅速发展,虚拟现实(Virtual Reality,VR)技术在近些年不断完善,其应用范围也变得十分广泛.为了搭建逼真的虚拟场景,VR技术一般都需要用到大量精美的图像和复杂的动作.因此, ...
- HTML5安全:CORS(跨域资源共享)简介
前言:像CORS对于现代前端这么重要的技术在国内基本上居然很少有人使用和提及,在百度或者Google上搜索CORS,搜到的中文文章基本都是另外一种卫星定位技术CORS的介绍,让我等前端同学情何以堪(对 ...
- X窗口系统的协议和架构
转自X窗口系统的协议和架构 在电脑中,X窗口系统(常称作 X11.X)是一种以位图显示的网络透明化窗口系统.本条目详述 X11 的协议及其技术架构. X C/S模型和网络透明性 X 基于C/S模型.运 ...