Fragment 碎片转换界面(2018.5.31)

package com.example.zy1;


import com.example.home.fragment1;
import com.example.home.fragment2;
import com.example.home.fragment3;
import com.example.home.fragment4;


import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {


private fragment1 f1;
private fragment2 f2;
private fragment3 f3;
private fragment4 f4;
private FragmentManager fm;
private FragmentManager ffm;
private FragmentManager fffm;
private FragmentManager ffmm;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

f1 = new fragment1();
f2 = new fragment2();
f3 = new fragment3();
f4 = new fragment4();

//首页
fm = getFragmentManager();
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction bt = fm.beginTransaction();
bt.replace(R.id.main_l1, f1);
bt.commit();
}
});

ffm = getFragmentManager();
findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction bt = ffm.beginTransaction();
bt.replace(R.id.main_l1, f2);
bt.commit();
}
});


fffm = getFragmentManager();
findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction bt = fffm.beginTransaction();
bt.replace(R.id.main_l1,f3);
bt.commit();
}
});


ffmm = getFragmentManager();
findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction bt = ffmm.beginTransaction();
bt.replace(R.id.main_l1,f4);
bt.commit();
}
});

}

}


package com.example.home;




import com.example.zy1.R;


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
View v = inflater.inflate(R.layout.sy, null);
return v;
}

}


package com.example.home;


import com.example.zy1.R;


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{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.txl, null);
return v;
}


}


package com.example.home;


import com.example.zy1.R;


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{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.fx, null);
return v;
}


}


package com.example.home;


import com.example.zy1.R;


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{
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.wd, null);
return v;
}

}



<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"
    tools:context="${relativePackage}.${activityClass}"
    android:orientation="vertical"
     >


    <FrameLayout 
        android:id="@+id/main_l1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
android:background="#000000"
        ></FrameLayout>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"  
        android:orientation="horizontal"            
        >
        <Button 
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="首页"
            />
        <Button 
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="通讯录"
            />
        <Button 
            android:id="@+id/btn3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="发现"
            />
        <Button 
            android:id="@+id/btn4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:text="我的"
            />
    </LinearLayout>


</LinearLayout>


<?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" >
    
    <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="发现"
        android:textColor="#FFFFFF"
        />


</LinearLayout>



<?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" >
    
<TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="首页"
        android:gravity="center"
        android:textColor="#FFFFFF"
        />
    

</LinearLayout>


<?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" >
    
     <TextView 
         
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="通讯录"
        android:gravity="center"
        android:textColor="#FFFFFF"
        />


</LinearLayout>



<?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" >
    
     <TextView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我的"
        android:gravity="center"
        android:textColor="#FFFFFF"
        />


</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值