选项卡切换
2016-04-10 10:56
417 查看
1、视图
2、fragment的视图如下
3、切换选项卡代码
4、fragment的代码类似如下
<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:gravity="center" android:id="@+id/tab1" android:layout_weight="1" android:layout_width="0dip" android:layout_height="wrap_content" android:text="社会新闻" /> <TextView android:gravity="center" android:id="@+id/tab2" android:layout_weight="1" android:layout_width="0dip" android:layout_height="wrap_content" android:text="生活新闻" /> <TextView android:gravity="center" android:id="@+id/tab3" android:layout_weight="1" android:layout_width="0dip" android:layout_height="wrap_content" android:text="军事新闻" /> <TextView android:gravity="center" android:id="@+id/tab4" android:layout_weight="1" android:layout_width="0dip" android:layout_height="wrap_content" android:text="娱乐新闻" /> </LinearLayout> <LinearLayout android:id="@+id/content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center" > </LinearLayout> </LinearLayout>
2、fragment的视图如下
<?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="生活新闻" /> </LinearLayout>
3、切换选项卡代码
package com.itheimazyh.changeui; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener { private TextView tv1,tv2,tv3,tv4; private List<TextView> tvList; private FragmentManager fm; private FragmentTransaction ft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.tab1); tv2 = (TextView) findViewById(R.id.tab2); tv3 = (TextView) findViewById(R.id.tab3); tv4 = (TextView) findViewById(R.id.tab4); tvList = new ArrayList<TextView>(); tvList.add(tv1); tvList.add(tv2); tvList.add(tv3); tvList.add(tv4); tv1.setBackgroundColor(Color.BLUE); fm = getFragmentManager(); ft = fm.beginTransaction(); ft.replace(R.id.content, new Fragment1()); ft.commit(); tv1.setOnClickListener(this); tv2.setOnClickListener(this); tv3.setOnClickListener(this); tv4.setOnClickListener(this); } @Override public void onClick(View v) { for(TextView tv : tvList){ tv.setBackgroundColor(Color.WHITE); } ft = fm.beginTransaction(); switch(v.getId()){ case R.id.tab1: tv1.setBackgroundColor(Color.BLUE); ft.replace(R.id.content, new Fragment1()); break; case R.id.tab2: tv2.setBackgroundColor(Color.BLUE); ft.replace(R.id.content, new Fragment2()); break; case R.id.tab3: tv3.setBackgroundColor(Color.BLUE); ft.replace(R.id.content, new Fragment3()); break; case R.id.tab4: tv4.setBackgroundColor(Color.BLUE); ft.replace(R.id.content, new Fragment4()); break; } ft.commit(); } }
4、fragment的代码类似如下
package com.example.myfragment3; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment1, null); } }
相关文章推荐
- Codeforces--658A--Bear and Reverse Radewoosh(模拟)
- shell 之 for 循环
- iOS Md5编码
- 怎样加入cocostudio生成的UI到项目
- Java学习系列——HTTP协议
- 指针与数组
- filter 转换字符编码
- HDOJ-1695 GCD
- 面向对象与面向过程的区别
- Purelayout小结1
- Python文件写操作
- 背包问题:01 完全 多重
- mysql_error() mysql_errno() 错误代码1064
- MySql中存储过程中的@变量总是无法执行,提示Parameter '@XXX' must be defined
- 随便取十个数1
- Javascript实现苹果悬浮虚拟按钮
- 【UE4学习】05——Vive的环境搭建
- [从头学数学] 第170节 空间几何体
- 【iOS】一种仿京东搜索历史记录的表格的实现(sqlite3的使用)
- 整理Ruby相关的各种概念(rvm, gem, bundle, rake, rails等)