1.话不多少直接上代码(仅供参考)
这是我们的布局文件
<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">
<FrameLayout
android:id="@+id/fragm"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
/>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/bt01"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="首页"
android:button="@null"
android:gravity="center"
/>
<RadioButton
android:id="@+id/bt02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="西瓜视频"
android:button="@null"
android:gravity="center"
/>
<RadioButton
android:id="@+id/bt03"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="微头条"
android:button="@null"
android:gravity="center"
/>
<RadioButton
android:id="@+id/bt04"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="首页"
android:button="@null"
android:gravity="center"
/>
</RadioGroup>
</LinearLayout>
主页面的代码
package com.example.zhongliumoni;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class MainActivity extends FragmentActivity {
private Fragment01 fragment01;
private Fragment02 fragment02;
private Fragment03 fragment03;
private Fragment04 fragment04;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源id
rg = findViewById(R.id.rg);
//new出fragment实例
fragment01 = new Fragment01();
fragment02 = new Fragment02();
fragment03 = new Fragment03();
fragment04 = new Fragment04();
//得到事物
FragmentTransaction add1 = getSupportFragmentManager().beginTransaction().add(R.id.fragm,fragment01);
FragmentTransaction add2 = getSupportFragmentManager().beginTransaction().add(R.id.fragm, fragment02);
FragmentTransaction add3 = getSupportFragmentManager().beginTransaction().add(R.id.fragm, fragment03);
FragmentTransaction add4 = getSupportFragmentManager().beginTransaction().add(R.id.fragm, fragment04);
//提交事物
add4.commit();
add3.commit();
add2.commit();
add1.commit();
//默认显示
// getSupportFragmentManager().beginTransaction().hide(fragment02).hide(fragment03).hide(fragment04).show(fragment01).commit();
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.bt01:
getSupportFragmentManager().beginTransaction().hide(fragment02).hide(fragment03).hide(fragment04).show(fragment01).commit();
break;
case R.id.bt02:
getSupportFragmentManager().beginTransaction().hide(fragment01).hide(fragment03).hide(fragment04).show(fragment02).commit();
break;
case R.id.bt03:
getSupportFragmentManager().beginTransaction().hide(fragment01).hide(fragment02).hide(fragment04).show(fragment03).commit();
break;
case R.id.bt04:
getSupportFragmentManager().beginTransaction().hide(fragment01).hide(fragment02).hide(fragment03).show(fragment04).commit();
break;
default:
break;
}
}
});
}
}