本文结合之前的动态创建fragment来进行一个实践,来实现用Fragment创建一个选项卡

本文地址:http://www.cnblogs.com/wuyudong/p/5898075.html,转载请注明源地址。

项目布局

<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="fill_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="社会新闻" /> <TextView
android:id="@+id/tab2"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="生活新闻" /> <TextView
android:id="@+id/tab3"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="军事新闻" /> <TextView
android:id="@+id/tab4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐新闻" />
</LinearLayout> <LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout> </LinearLayout>

新建Fragment1.java~Fragment4.java,其中Fragment1.java中的代码如下:

public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, null);
} }

其他几个文件的代码类似

新建fragment1.xml~fragment4.xml,其中fragment1.xml中的代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" > <TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="社会新闻"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>

其他几个文件的代码类似

MainActivity.java中的代码如下:

public class MainActivity extends Activity implements OnClickListener {

    private LinearLayout content;
private TextView tv1, tv2, tv3, tv4;
private FragmentManager fm;
private FragmentTransaction ft; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); content = (LinearLayout) findViewById(R.id.content); tv1 = (TextView) findViewById(R.id.tab1);
tv2 = (TextView) findViewById(R.id.tab2);
tv3 = (TextView) findViewById(R.id.tab3);
tv4 = (TextView) findViewById(R.id.tab4); tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
tv3.setOnClickListener(this);
tv4.setOnClickListener(this); fm = getFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.content, new Fragment1()); // 默认情况下Fragment1 } @Override
public void onClick(View v) {
ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.tab1:
ft.replace(R.id.content, new Fragment1());
break;
case R.id.tab2:
ft.replace(R.id.content, new Fragment2());
break;
case R.id.tab3:
ft.replace(R.id.content, new Fragment3());
break;
case R.id.tab4:
ft.replace(R.id.content, new Fragment4());
break; default:
break;
}
ft.commit(); } }

运行项目后如下效果:

Android 用Fragment创建一个选项卡的更多相关文章

  1. android2.3 View视图框架源码分析之一:android是如何创建一个view的?

    View是所有控件的一个基类,无论是布局(Layout),还是控件(Widget)都是继承自View类.只不过layout是一个特殊的view,它里面创建一个view的数组可以包含其他的view而已. ...

  2. android 之fragment创建

    1.使用xml标签 1.1定义两个重要属性  <fragment         android:id="@+id/fregment_top"        android: ...

  3. Creating an Android Project(创建一个android项目)

    一个android项目包含了你的应用程序中的所有源代码文件,我们可以通过android sdk tools轻松地创建一个拥有默认文件跟文件夹的android项目. 这部分课程我们将展示两种创建andr ...

  4. 【译】用Fragment创建动态的界面布局(附Android示例代码)

    原文链接:Building a Dynamic UI with Fragments 为了在Android上创建一个动态和多视图的用户界面,你需要封装UI控件和模块化Activity的行为,以便于你能够 ...

  5. Android 创建一个新的Activity

    本文转载自:http://www.cnblogs.com/wuyudong/p/5658020.html 新建一个项目,创建一个java类,继承自Activity 新建一个Android.xml文件: ...

  6. 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)

    发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...

  7. 1.1、Android Studio创建一个项目

    Android Studio中的项目包含一个或多个模块.本节帮助你创建一个新的项目. 创建一个新的项目 如果你之前没有打开项目,Android Studio显示欢迎页面,通过点击Start a New ...

  8. 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行

    需求说明: 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行 实现步骤: 打开 Android Studio,创建一个 Andr ...

  9. android的fragment基本介绍

    可以分为下面的几部分: 使用支持库 创建一个Fragment 创建一个动态UI 多个Fragment之间的通信 1.使用支持库 如果您的应用需要运行在3.0及以上的版本,可以忽略这部分内容. 如果您的 ...

随机推荐

  1. 我如何调优SQL Server查询

    我是个懒人,我只想干尽可能少的活.当我干活的时候我不想太多.是,你没看错,这看起来很糟糕,作为一个DBA这很不合格.但在今天的文章里,我想给你展示下,当你想对特定查询创建索引设计时,你如何把你的工作和 ...

  2. 在SQL Server 2016里使用查询存储进行性能调优

    作为一个DBA,排除SQL Server问题是我们的职责之一,每个月都有很多人给我们带来各种不能解释却要解决的性能问题. 我就多次听到,以前的SQL Server的性能问题都还好且在正常范围内,但现在 ...

  3. flexbox简介

    flexbox简介 什么是flexbox flexbox是一种新的布局方式,这种布局方式是2009年W3C提出的方案.它可以简便,完整,完成页面的布局.目前,它已经得到所有浏览器的支持. 但是flex ...

  4. 关于WEB Service&WCF&WebApi实现身份验证之WCF篇(1)

    WCF身份验证一般常见的方式有:自定义用户名及密码验证.X509证书验证.ASP.NET成员资格(membership)验证.SOAP Header验证.Windows集成验证.WCF身份验证服务(A ...

  5. 实现多表关联来方便你的SELECT查询功能

    这次来学习一下多表关联.比如在数据中创建下面几张表,你可以从主键PRIMARY KEY与外键FOREIGN KEY来看到这几张表它们之间的关系. 场所表[dbo].[SixS_Location]: 主 ...

  6. 从C#到Objective-C,循序渐进学习苹果开发(1)--准备开发账号和开发环境

    本随笔系列主要介绍从一个Windows平台从事C#开发到Mac平台苹果开发的一系列感想和体验历程,本系列文章是在起步阶段逐步积累的,希望带给大家更好,更真实的转换历程体验,因为一旦方方面面都精通了,也 ...

  7. C# winform控件之listview学习积累

    //1.用key给ListViewItem 的 SubItems赋值 ListViewItem listViewItem= listView1.Items.Add("第一列文字") ...

  8. 鼠标滑动加载div

  9. AFNetworking 3.1

    听说之后AFHttpWorking版本可能会影响到苹果的审核,今天下了最新版本的AFHttpWorking,并且做了简单的封装,我这里是通过cocoapods下载了两个工具 1=AFHttpWorki ...

  10. Verilog学习笔记设计和验证篇(五)...............层次化事件队列

    详细的了解层次化事件队列有助于理解Verilog的阻塞赋值和非阻塞赋值功能.所谓层次化事件队列指的是用于调度仿真时间的不同Verilog事件队列.在IEEE的5.3节中定义了层次化事件队列在逻辑上分为 ...