选项卡切换
1、视图
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 8 <LinearLayout 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:orientation="horizontal" 12 > 13 <TextView 14 android:gravity="center" 15 android:id="@+id/tab1" 16 android:layout_weight="1" 17 android:layout_width="0dip" 18 android:layout_height="wrap_content" 19 android:text="社会新闻" 20 /> 21 <TextView 22 android:gravity="center" 23 android:id="@+id/tab2" 24 android:layout_weight="1" 25 android:layout_width="0dip" 26 android:layout_height="wrap_content" 27 android:text="生活新闻" 28 /> 29 <TextView 30 android:gravity="center" 31 android:id="@+id/tab3" 32 android:layout_weight="1" 33 android:layout_width="0dip" 34 android:layout_height="wrap_content" 35 android:text="军事新闻" 36 /> 37 <TextView 38 android:gravity="center" 39 android:id="@+id/tab4" 40 android:layout_weight="1" 41 android:layout_width="0dip" 42 android:layout_height="wrap_content" 43 android:text="娱乐新闻" 44 /> 45 </LinearLayout> 46 <LinearLayout 47 android:id="@+id/content" 48 android:layout_width="fill_parent" 49 android:layout_height="fill_parent" 50 android:orientation="horizontal" 51 android:gravity="center" 52 > 53 54 </LinearLayout> 55 56 </LinearLayout>
2、fragment的视图如下
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:gravity="center" 6 android:orientation="vertical" > 7 8 <TextView 9 android:id="@+id/textView1" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:text="生活新闻" /> 13 14 </LinearLayout>
3、切换选项卡代码
1 package com.itheimazyh.changeui; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.os.Bundle; 7 import android.app.Activity; 8 import android.app.FragmentManager; 9 import android.app.FragmentTransaction; 10 import android.graphics.Color; 11 import android.view.Menu; 12 import android.view.View; 13 import android.view.View.OnClickListener; 14 import android.widget.TextView; 15 16 public class MainActivity extends Activity implements OnClickListener { 17 private TextView tv1,tv2,tv3,tv4; 18 private List<TextView> tvList; 19 private FragmentManager fm; 20 private FragmentTransaction ft; 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 super.onCreate(savedInstanceState); 24 setContentView(R.layout.activity_main); 25 26 tv1 = (TextView) findViewById(R.id.tab1); 27 tv2 = (TextView) findViewById(R.id.tab2); 28 tv3 = (TextView) findViewById(R.id.tab3); 29 tv4 = (TextView) findViewById(R.id.tab4); 30 31 tvList = new ArrayList<TextView>(); 32 tvList.add(tv1); 33 tvList.add(tv2); 34 tvList.add(tv3); 35 tvList.add(tv4); 36 37 tv1.setBackgroundColor(Color.BLUE); 38 fm = getFragmentManager(); 39 ft = fm.beginTransaction(); 40 ft.replace(R.id.content, new Fragment1()); 41 ft.commit(); 42 43 tv1.setOnClickListener(this); 44 tv2.setOnClickListener(this); 45 tv3.setOnClickListener(this); 46 tv4.setOnClickListener(this); 47 } 48 @Override 49 public void onClick(View v) { 50 for(TextView tv : tvList){ 51 tv.setBackgroundColor(Color.WHITE); 52 } 53 ft = fm.beginTransaction(); 54 55 switch(v.getId()){ 56 case R.id.tab1: 57 tv1.setBackgroundColor(Color.BLUE); 58 ft.replace(R.id.content, new Fragment1()); 59 break; 60 case R.id.tab2: 61 tv2.setBackgroundColor(Color.BLUE); 62 ft.replace(R.id.content, new Fragment2()); 63 break; 64 case R.id.tab3: 65 tv3.setBackgroundColor(Color.BLUE); 66 ft.replace(R.id.content, new Fragment3()); 67 break; 68 case R.id.tab4: 69 tv4.setBackgroundColor(Color.BLUE); 70 ft.replace(R.id.content, new Fragment4()); 71 break; 72 } 73 ft.commit(); 74 75 } 76 77 }
4、fragment的代码类似如下
1 package com.example.myfragment3; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 public class Fragment1 extends Fragment { 10 11 @Override 12 public View onCreateView(LayoutInflater inflater, ViewGroup container, 13 Bundle savedInstanceState) { 14 // TODO Auto-generated method stub 15 return inflater.inflate(R.layout.fragment1, null); 16 } 17 18 }

浙公网安备 33010602011771号