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. PL/SQL学习(一)

    原文参考:http://plsql-tutorial.com/ 组成: 声明部分(可选) 执行部分(必选) 异常处理(可选)       声明:         DECLARE       执行:   ...

  2. 用javascript操作xml(二)JavaScript 将XML转换成字符串(xml to string)

    function xmlToString(xmlData) { var xmlString; //IE if (window.ActiveXObject){ xmlString = xmlData.x ...

  3. [转]EJS入门

    今天学习了EJS,转个再点个赞,动态创网页的好方法! 主页:http://www.embeddedjs.com/ 转自:http://www.csser.com/board/4fddc4f0b28ed ...

  4. POJ 1961 Period KMP算法next数组的应用

    题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...

  5. 【转】mybatis 获取自增id

    转自:http://www.cnblogs.com/rhythmK/p/4047142.html 1.环境: mybatis : 3.2.3 spring-mybatis:  1.2.1 mysql: ...

  6. MYSQL ERROR 1130: Host is not allowed to connect to this MySQL server

    今天安装MYSQL遇到MYSQL ERROR 1130: Host is not allowed to connect to this MySQL server, 试了很多办法都不行 skip-gra ...

  7. avalon 中require.config源码分析

    /********************************************************************* * 配置系统 在系统运行的开始就需要读取系统中requir ...

  8. 获取ListControl控件中(复选框)CheckBox的状态

    原文地址:http://blog.chinaunix.net/uid-20680966-id-1896376.html 1 建立测试工程      新建一个对话框工程,并添加一个CListCtrl控件 ...

  9. Oracle 基础知识

    SQLDevelop 1. 查看数据库版本 :  select  *  from   v$version; 2. 查看表结构:        desc     TABLE_NAME 3. 查看当前连接 ...

  10. [转贴]关于C++的抽象的一点新认识

    http://my.oschina.net/fzyz999/blog/138491 关于本文 本文是笔者在阅读<C++沉思录>第0章——序幕后的一点想法,可以算作是笔记也可以算作是读后感. ...