Android 用Fragment创建一个选项卡
本文结合之前的动态创建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创建一个选项卡的更多相关文章
- android2.3 View视图框架源码分析之一:android是如何创建一个view的?
View是所有控件的一个基类,无论是布局(Layout),还是控件(Widget)都是继承自View类.只不过layout是一个特殊的view,它里面创建一个view的数组可以包含其他的view而已. ...
- android 之fragment创建
1.使用xml标签 1.1定义两个重要属性 <fragment android:id="@+id/fregment_top" android: ...
- Creating an Android Project(创建一个android项目)
一个android项目包含了你的应用程序中的所有源代码文件,我们可以通过android sdk tools轻松地创建一个拥有默认文件跟文件夹的android项目. 这部分课程我们将展示两种创建andr ...
- 【译】用Fragment创建动态的界面布局(附Android示例代码)
原文链接:Building a Dynamic UI with Fragments 为了在Android上创建一个动态和多视图的用户界面,你需要封装UI控件和模块化Activity的行为,以便于你能够 ...
- Android 创建一个新的Activity
本文转载自:http://www.cnblogs.com/wuyudong/p/5658020.html 新建一个项目,创建一个java类,继承自Activity 新建一个Android.xml文件: ...
- 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)
发布在我的网站 http://kesenhoo.github.io/blog/2013/06/30/android-training-ui-creating-custom-views-lesson-1 ...
- 1.1、Android Studio创建一个项目
Android Studio中的项目包含一个或多个模块.本节帮助你创建一个新的项目. 创建一个新的项目 如果你之前没有打开项目,Android Studio显示欢迎页面,通过点击Start a New ...
- 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行
需求说明: 使用 Android Studio 开发工具创建一个 Android 应用程序,并在 Genymotion 模拟器上运行 实现步骤: 打开 Android Studio,创建一个 Andr ...
- android的fragment基本介绍
可以分为下面的几部分: 使用支持库 创建一个Fragment 创建一个动态UI 多个Fragment之间的通信 1.使用支持库 如果您的应用需要运行在3.0及以上的版本,可以忽略这部分内容. 如果您的 ...
随机推荐
- asp.Net获取脚本传过来的参数的方法汇总
最基础的知识啦,不过,还是记下来吧. 接收用get 方法传输的数据的写法: string userName= Request.QueryString["name"]; 接收用pos ...
- html/css基础篇——link和@inport详解以及脚本执行顺序探讨
先说一说两者之间的异同 两者都可以引用外部CSS的方式,现在主流浏览器两者都支持(ps:@import是CSS2.1提出的),但是存在一定的区别: 1.link是XHTML标签,除了加载CSS外,还可 ...
- 第22/24周 等待和I/O延迟统计
大家好,欢迎回到性能调优培训的第22周.上周我谈了SQL Server里的基线,今天我们继续,谈下SQL Server里的等待和I/O延迟统计.当我进行SQL服务器健康检查时,我总会使用这2个维度全局 ...
- 27 个免费的 HTML5/CSS3 模板供下载
EscapEvelocity Responsive Html5 Theme ( Demo || Download) Base 2013 Responsive Html5 Theme (Demo || ...
- AMD64和i386的区别
下载Debian系统时,出现两个选项:ADM64和i386,那么这两者的区别是什么? i386=Intel 80386.其实i386通常被用来作为对Intel(英特尔)32位微处理器的统称. AMD6 ...
- ASP.NET Core Web API 开发-RESTful API实现
ASP.NET Core Web API 开发-RESTful API实现 REST 介绍: 符合REST设计风格的Web API称为RESTful API. 具象状态传输(英文:Representa ...
- WinForm打印
WinForm打印要用打印控件: PageSetupDialog:打印设置对话框 PrintDialog:打印对话框 PrintDocument:要打印的对象,非常重要 PrintPreviewCon ...
- C# Winform MD5加密学习积累
string password = txtPassword.Text.Trim(); byte[] result = Encoding.Default.GetBytes(password); MD5 ...
- 【C#】第3章补充(二)如何将图形作为对象
分类:C#.VS2015 创建日期:2016-06-23 使用教材:(十二五国家级规划教材)<C#程序设计及应用教程>(第3版) 一.要点 该例子属于高级技术中的基本用法.对于初学者来说这 ...
- Delegate
public delegate void EventHandler(object sender, EventArgs e); pulic EventHandler HandleMapMessage; ...