Android笔记 fragment的Tab样式demo
2014-10-22 19:17
134 查看
注:android:minSdkVersion="11"
11才有的新特性
此种效果可以用TabHost实现 也可以用ViewPager实现(ViewPager没实现过 据说更美化)
demo
1布局
activity_main.xml
fragment1.xml
fragment2.xml
fragment3.xml
fragment4.xml
2class
Fragment1.java
Fragment2.java
Fragment3.java
Fragment4.java
MainActivity.java
11才有的新特性
此种效果可以用TabHost实现 也可以用ViewPager实现(ViewPager没实现过 据说更美化)
demo
1布局
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="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" android:orientation="vertical" > </LinearLayout> </LinearLayout>
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="我是界面1" /> </LinearLayout>
fragment2.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="我是界面2" /> </LinearLayout>
fragment3.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="我是界面3" /> </LinearLayout>
fragment4.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="我是界面4" /> </LinearLayout>
2class
Fragment1.java
package com.example.a120fragmentchangeui; 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); } }
Fragment2.java
package com.example.a120fragmentchangeui; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment2 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment2, null); } }
Fragment3.java
package com.example.a120fragmentchangeui; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment3 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment3, null); } }
Fragment4.java
package com.example.a120fragmentchangeui; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment4 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.fragment4, null); } }
MainActivity.java
package com.example.a120fragmentchangeui; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.graphics.Color; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.TextView; 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); tv1.setBackgroundColor(Color.BLUE); fm = getFragmentManager(); ft = fm.beginTransaction(); ft.replace(R.id.content, new Fragment1()); ft.commit(); } @Override public void onClick(View v) { ft = fm.beginTransaction(); // TODO Auto-generated method stub switch (v.getId()) { case R.id.tab1: tv1.setBackgroundColor(Color.BLUE); tv2.setBackgroundColor(Color.WHITE); tv3.setBackgroundColor(Color.WHITE); tv4.setBackgroundColor(Color.WHITE); ft.replace(R.id.content, new Fragment1()); break; case R.id.tab2: tv1.setBackgroundColor(Color.WHITE); tv2.setBackgroundColor(Color.BLUE); tv3.setBackgroundColor(Color.WHITE); tv4.setBackgroundColor(Color.WHITE); ft.replace(R.id.content, new Fragment2()); break; case R.id.tab3: tv1.setBackgroundColor(Color.WHITE); tv2.setBackgroundColor(Color.WHITE); tv3.setBackgroundColor(Color.BLUE); tv4.setBackgroundColor(Color.WHITE); ft.replace(R.id.content, new Fragment3()); break; case R.id.tab4: tv1.setBackgroundColor(Color.WHITE); tv2.setBackgroundColor(Color.WHITE); tv3.setBackgroundColor(Color.WHITE); tv4.setBackgroundColor(Color.BLUE); ft.replace(R.id.content, new Fragment4()); break; default: break; } ft.commit(); } }
相关文章推荐
- 自学android笔记之TabLayout结合ViewPager和Fragment实现多页面滑动
- Android笔记 fragment入门 动态加载fragment demo+ 判断横竖屏
- Android笔记--Fragment获取父Activity的控件并修改内容demo
- Android Fragment学习笔记(二)----Fragment界面添加和管理
- 友善之臂Mini6410之Android开发学习笔记(4)-EEPROM Demo
- Android: 自定义Tab样式
- 自定义android的tab样式
- APIDemo学习笔记——Android上几种简单的Animation使用方法(三)——3D Transition
- Android Fragment学习笔记(二)----Fragment界面添加和管理
- 友善之臂Mini6410之Android开发学习笔记(2)-PWM Demo
- 自定义android的tab样式
- Android Fragment学习笔记(一)
- Android TabActivity——学习笔记
- Android Fragment学习笔记(二)
- (android实战)自定义android的tab样式--转载
- CSS学习笔记-附加篇( 新浪大通栏Tab样式)
- APIDemo学习笔记——Android上几种简单的Animation使用方法(二)
- Android: 自定义Tab样式
- 自定义android的tab样式
- 自定义android的tab样式