XML文件
<FrameLayout
android:id="@+id/ft1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/fistrl"
android:layout_weight="1">
</FrameLayout>
java文件
public class MainActivity extends FragmentActivity {
private RelativeLayout bt_rl1,bt_rl2,bt_rl3,bt_rl4,top_rlt1;private ImageView bt_iv1,bt_iv2,bt_iv3,bt_iv4;
private TextView bt_tv1,bt_tv2,bt_tv3,bt_tv4,main_tv,main_tvl,main_tvr;
private FragmentHome home;
private FragmentList listl;
private FragmentMydoctor mydoctor;
private FragmentMore more;
FragmentTransaction transaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initBt();
enabledFalse();
show();
}
//初始化id
public void initBt() {
bt_rl1 = (RelativeLayout) findViewById(R.id.bt_rl1);
bt_rl2 = (RelativeLayout) findViewById(R.id.bt_rl2);
bt_rl3 = (RelativeLayout) findViewById(R.id.bt_rl3);
bt_rl4 = (RelativeLayout) findViewById(R.id.bt_rl4);
top_rlt1 = (RelativeLayout) findViewById(R.id.toprlt1);
bt_iv1 = (ImageView) findViewById(R.id.bt_iv1);
bt_iv2 = (ImageView) findViewById(R.id.bt_iv2);
bt_iv3 = (ImageView) findViewById(R.id.bt_iv3);
bt_iv4 = (ImageView) findViewById(R.id.bt_iv4);
bt_tv1 = (TextView) findViewById(R.id.bt_tv1);
bt_tv2 = (TextView) findViewById(R.id.bt_tv2);
bt_tv3 = (TextView) findViewById(R.id.bt_tv3);
bt_tv4 = (TextView) findViewById(R.id.bt_tv4);
main_tv = (TextView) findViewById(R.id.main_tv);
main_tvl = (TextView) findViewById(R.id.main_tvl);
main_tvr = (TextView) findViewById(R.id.main_tvr);
}
public void show(){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
bt_iv1.setEnabled(true);
bt_tv1.setTextColor(Color.rgb(39,174,96));
main_tv.setText("丁香医生");
if (home == null) {
home = new FragmentHome();
transaction.replace(R.id.ft1, home);
}else{
transaction.replace(R.id.ft1, home);
}
transaction.commit();
}
public void ck1(View view){
top_rlt1.setVisibility(View.VISIBLE);
show();
}
public void ck2(View view){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
top_rlt1.setVisibility(View.GONE);
bt_iv2.setEnabled(true);
bt_tv2.setTextColor(Color.rgb(39,174,96));
main_tv.setText("健康科普");
if (listl == null) {
listl = new FragmentList();
transaction.replace(R.id.ft1, listl);
}else{
transaction.replace(R.id.ft1, listl);
}
transaction.commit();
}
public void ck3(View view){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
top_rlt1.setVisibility(View.GONE);
bt_iv3.setEnabled(true);
bt_tv3.setTextColor(Color.rgb(39,174,96));
main_tv.setText("家人健康");
main_tvl.setText("同步");
main_tvr.setText("编辑");
if (mydoctor == null) {
mydoctor = new FragmentMydoctor();
transaction.replace(R.id.ft1, mydoctor);
}else{
transaction.replace(R.id.ft1, mydoctor);
}
transaction.commit();
}
public void ck4(View view){
hideFragment();
//管理器要用get方式获取
FragmentManager manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
enabledFalse();
top_rlt1.setVisibility(View.GONE);
bt_iv4.setEnabled(true);
bt_tv4.setTextColor(Color.rgb(39,174,96));
main_tv.setText("更多");
if (more == null) {
more = new FragmentMore();
transaction.replace(R.id.ft1, more);
}else{
transaction.replace(R.id.ft1, more);
}
transaction.commit();
}
//设置一个隐藏所有Fragmnet的方法
public void hideFragment() {
if (home != null) {
transaction.hide(home);
}
if (listl != null) {
transaction.hide(listl);
}
if (mydoctor != null) {
transaction.hide(mydoctor);
}
if (more != null) {
transaction.hide(more);
}
}
//初始化按键
public void enabledFalse(){
bt_iv1.setEnabled(false);
bt_iv2.setEnabled(false);
bt_iv3.setEnabled(false);
bt_iv4.setEnabled(false);
bt_tv1.setTextColor(Color.rgb(153,153,153));
bt_tv2.setTextColor(Color.rgb(153,153,153));
bt_tv3.setTextColor(Color.rgb(153,153,153));
bt_tv4.setTextColor(Color.rgb(153,153,153));
main_tv.setText("");
main_tvl.setText("");
main_tvr.setText("");
}
}