鱼C论坛

 找回密码
 立即注册
查看: 2331|回复: 7

[原创]android学习之fragmentTab

[复制链接]
发表于 2014-11-22 20:57:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 青玄 于 2014-11-22 21:00 编辑

本案例主要实现的是不同页面之间的切换操作,其实就是在原有的界面上进行一个界面的替换操作而已,本来activity可以实现这个功能,但是太多的activity使得程序过于臃肿,所以用fragment进行切换,可以减少这个弊端;
一下是主要实现的步骤,当然只是一部分而已:
1. 开始出现的界面:main_activity.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical"
  6.     tools:context=".MainActivity" >

  7.     <fragment
  8.         android:id="@+id/tab"
复制代码
2. TabFragment.java文件主要实现加载标题栏的布局文件:
  1. package com.example.tabfragment_test;

  2. import android.app.Fragment;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;

  7. public class TabFragment extends Fragment {

  8.         @Override
  9.         public View onCreateView(LayoutInflater inflater, ViewGroup container,
  10.                         Bundle savedInstanceState) {
  11.                 // TODO Auto-generated method stub
  12.                 View view = inflater.inflate(R.layout.fragment_tab, null);
  13.                 return view;
  14.         }

  15.         
  16. }
复制代码

3.加载标题栏的布局文件:fragment_tab.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="#55ff66"
  6.     android:gravity="center"
  7.     android:orientation="horizontal"
  8.    
  9.     tools:context=".MainActivity" >

  10.     <TextView
  11.         android:gravity="center"
  12.         android:layout_weight="1"
  13.         android:layout_width="0dip"
  14.         android:layout_height="wrap_content"
  15.         android:id="@+id/tv1"
  16.         android:text="小甲鱼"
  17.          />
  18.    
  19.      <TextView
  20.         android:gravity="center"
  21.         android:layout_weight="1"
  22.         android:layout_width="0dip"
  23.         android:layout_height="wrap_content"
  24.         android:id="@+id/tv2"
  25.         android:text="小白马"
  26.          />
  27.      
  28.       <TextView
  29.         android:gravity="center"
  30.         android:layout_weight="1"
  31.         android:layout_width="0dip"
  32.         android:layout_height="wrap_content"
  33.         android:id="@+id/tv3"
  34.         android:text="小仙"
  35.          />
  36.       
  37.        <TextView
  38.         android:gravity="center"
  39.         android:layout_weight="1"
  40.         android:layout_width="0dip"
  41.         android:layout_height="wrap_content"
  42.         android:id="@+id/tv4"
  43.         android:text="wiliam"
  44.          />
  45. </LinearLayout>
复制代码

4.在MainActivity.java主程序中进行监听切换操作:
  1. package com.example.tabfragment_test;

  2. import com.cbd.tabfragment.Fragment1;
  3. import com.cbd.tabfragment.Fragment2;
  4. import com.cbd.tabfragment.Fragment3;
  5. import com.cbd.tabfragment.Fragment4;

  6. import android.os.Bundle;
  7. import android.app.Activity;
  8. import android.app.FragmentManager;
  9. import android.app.FragmentTransaction;
  10. import android.content.DialogInterface;
  11. import android.graphics.Color;

  12. import android.view.Menu;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.TextView;

  16. public class MainActivity extends Activity implements OnClickListener{

  17.         private TextView tv1;
  18.         private FragmentManager manager;
  19.         
  20.         @Override
  21.         protected void onCreate(Bundle savedInstanceState) {
  22.                 super.onCreate(savedInstanceState);
  23.                 setContentView(R.layout.activity_main);
  24.                
  25.                 this.tv1 = (TextView) this.findViewById(R.id.tv1);

  26.                 //对这个textView 进行监听
  27.                 tv1.setOnClickListener(this);  

  28.                 manager = this.getFragmentManager();    //获得fragment管理器
  29.         }

  30.         @Override
  31.         public void onClick(View v) {
  32.                 // TODO Auto-generated method stub
  33.                 FragmentTransaction ft = manager.beginTransaction();   //得到这个对象调用replace方法进行替换操作
  34.                
  35.                 switch(v.getId())
  36.                 {
  37.                 case R.id.tv1:
  38.                         ft.replace(R.id.content, new Fragment1());
  39.                         tv1.setBackgroundColor(Color.BLUE);
  40.                         tv2.setBackgroundColor(34757495);
  41.                         tv3.setBackgroundColor(57891292);
  42.                         tv4.setBackgroundColor(46897932);
  43.                         break;
  44.                 }
  45.                 ft.commit();   //提交
  46.         }

  47. }
复制代码

QQ截图20141122203816.png QQ截图20141122203829.png QQ截图20141122205656.png QQ截图20141122205707.png


评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
拈花小仙 + 5 + 5 + 3 感谢楼主无私奉献!

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2014-11-22 21:37:32 | 显示全部楼层
谢谢 小白马玄玄~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-22 21:38:03 | 显示全部楼层
帮我这论坛第一灌水户也出出名哈~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-22 21:39:20 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-22 21:41:03 | 显示全部楼层
强烈支持楼主ing,看来我不过用功
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-23 14:14:35 | 显示全部楼层

呵呵! 小仙!咱两谁跟谁啊!写程序的时候,实在想不出什么来!就想到你们了!以前写的程序也是这样的!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-23 14:15:16 | 显示全部楼层
拈花小仙 发表于 2014-11-22 21:38
帮我这论坛第一灌水户也出出名哈~

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-11-23 14:26:54 | 显示全部楼层
百日维新 发表于 2014-11-22 21:41
强烈支持楼主ing,看来我不过用功

新新能不能也做个实例,加上偶啦啦~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-23 17:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表