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 多标签应用的更多相关文章

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

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

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

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

  3. Android Fragment应用实战

    现在Fragment的应用真的是越来越广泛了,之前Android在3.0版本加入Fragment的时候,主要是为了解决Android Pad屏幕比较大,空间不能充分利用的问题,但现在即使只是在手机上, ...

  4. Android Fragment

    1.Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期. 2.Fragment 生命周期: 首页 最新文章 在线课程 业界 开发 ...

  5. Android Fragment应用实战,使用碎片向ActivityGroup说再见

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/13171191 现在Fragment的应用真的是越来越广泛了,之前Android在3 ...

  6. Android Fragment 解析和使用

    Android Fragment的生命周期和Activity类似,实际可能会涉及到数据传递,onSaveInstanceState的状态保存,FragmentManager的管理和Transactio ...

  7. Android Fragment详解

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

  8. Android Fragment 实例

    Fragment是Android honeycomb 3.0新增的概念,在Android——Fragment介绍.Android Fragment使用.Android FragmentManage F ...

  9. 【转】基于Android Fragment功能的例子

    原文网址:http://blog.csdn.net/eyu8874521/article/details/8252216 通过最近空闲时候对Fragment的学习,尝试着写了一个小Demo,将在开发的 ...

随机推荐

  1. MFC 简单输出EXCEL - (OLE)

    三图胜千言: 就是酱紫: //打印领料表 void CKnifeDlgDlg::PrintCurUsedTabel(int order) { // TODO: Add your command han ...

  2. Boolean 布尔类型详解

    这是最简单的类型.boolean 表达了真值,可以为 TRUE 或 FALSE.两个都不区分大小写. 要明确地将一个值转换成 boolean,用 (bool)或者 (boolean) 来强制转换.但是 ...

  3. wamp介绍

    Wamp介绍      Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python,一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在 ...

  4. SQL SERVER 2008 使用TDE加密和解密

    SQL SERVER 2008 加密和解密,这样的文件在互联网上不胜枚举,本文的寓意还是一样,一为记录,二可以为开发者提供在实现过程中的注意事项. TDE: Transparent data encr ...

  5. iOS 9适配技巧

    中文快速导航: 1.iOS9网络适配_ATS:改用更安全的HTTPS(见Demo1) i. WHAT(什么是SSL/TLS?跟HTTP和HTTPS有什么关系) ii. WHY(以前的HTTP不是也能用 ...

  6. Memcached(二)Memcached Java API基础之MemcachedClient

    1. 构造函数 public MemcachedClient(InetSocketAddress[] ia) throws IOException; public MemcachedClient(Li ...

  7. Aircrack-ng官方文档翻译[中英对照]---Airmon-ng

    Aircrack-ng官方文档翻译---Airmon-ng Description[简介] This script can be used to enable monitor mode on wire ...

  8. Java多线程初学者指南(4):线程的生命周期

    与人有生老病死一样,线程也同样要经历开始(等待).运行.挂起和停止四种不同的状态.这四种状态都可以通过Thread类中的方法进行控制.下面给出了Thread类中和这四种状态相关的方法. // 开始线程 ...

  9. Dirichlet Process 和 Dirichlet Process Mixture模型

    Dirichlet Process 和 Dirichlet Process Mixture模型 [本文链接:http://www.cnblogs.com/breezedeus/archive/2012 ...

  10. Z-Stack 软件架构分析

    转自Z-Stack 软件架构分析 Z-Stack的main函数在Zmain.c中,总体上来说,它一共做了两件工作,一个是系统初始化,即有启动代码来初始化硬件系统和软件架构需要的各个模块,另一个作用就是 ...