由于Fragment是在android api 4.0之后出现的,所以会出现兼容性的问题,如果想继续使用的话则需要在AndroidMainfest.xml中修改android的最低版本为11。如果这样的话在一定程度会造成不变,所以为了解决这问题,所以就要让Fragment向下兼容。
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">
<LinearLayout android:layout_width="match_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="match_parent" android:layout_height="match_parent" android:gravity=”android” android:orientation="vertical">
</LinearLayout>
</LinearLayout>
|
2)分别建立四个用于显示的fragment标签的布局文件,分别如下:
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:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="生活新闻"/> </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:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="军事新闻"/> </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:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="娱乐新闻"/> </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:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="百度新闻"/> </LinearLayout>
|
3)分别建立四个对应的继承了Fragment类的子类用来加载之前建立的布局文件
Fragmen1.java
package zjh.android.fragmenttab;
import android.os.Bundle; import android.support.v4.app.Fragment; 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) { return inflater.inflate(R.layout.fragment1,null); }
}
|
Fragment2.java
package zjh.android.fragmenttab;
import android.os.Bundle; import android.support.v4.app.Fragment; 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) { return inflater.inflate(R.layout.fragment2,null); }
}
|
Fragment3.java
package zjh.android.fragmenttab;
import android.os.Bundle; import android.support.v4.app.Fragment; 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) { return inflater.inflate(R.layout.fragment3,null); }
}
|
Fragment4.java
package zjh.android.fragmenttab;
import android.os.Bundle; import android.support.v4.app.Fragment; 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) { return inflater.inflate(R.layout.fragment4,null); }
}
|
4)在生成的MainActivity.java中使其继承FragmentActivity,同时使其中的FragmentManager和FragmentTransactionandroid的包分别来自于support.v4.app.FragmentManager和android.support.v4.app.FragmentTransaction,具体实现代码如下:
package zjh.android.fragmenttab;
import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener { private TextView tab1,tab2,tab3,tab4; FragmentManager fm = null ; FragmentTransaction ft = null ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); this.tab1 = (TextView)super.findViewById(R.id.tab1); this.tab2 = (TextView)super.findViewById(R.id.tab2); this.tab3 = (TextView)super.findViewById(R.id.tab3); this.tab4 = (TextView)super.findViewById(R.id.tab4); this.tab1.setOnClickListener(this); this.tab2.setOnClickListener(this); this.tab3.setOnClickListener(this); this.tab4.setOnClickListener(this); fm = super.getSupportFragmentManager(); ft = fm.beginTransaction(); ft.replace(R.id.content,new Fragment1()); ft.commit(); } @Override public void onClick(View view) { ft = fm.beginTransaction(); switch (view.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; } ft.commit(); }
} |