Fragment分别title显示_Demo

来源:互联网 发布:帝国cms不会自动刷新吗 编辑:程序博客网 时间:2024/04/29 01:27

主Activity代码:

package com.example.fragmenttest10;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;public class MainActivity extends FragmentActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //调用切换Fragmnet方法        changeFragmnet(new Fragment1());    }    //监听的按钮方法:   public void weixinclick(View v){       changeFragmnet(new Fragment1());   }   //监听的按钮方法:   public void findclick(View v){       changeFragmnet(new Fragment2());   }   //监听的按钮方法:   public void meclick(View v){       changeFragmnet(new Fragment3());   }   //切换fragment的方法   public void changeFragmnet(Fragment fr){       //第一步:得到fragment管理类       FragmentManager manager = getSupportFragmentManager();       //得到事务       FragmentTransaction beginTransaction = manager.beginTransaction();       //调用replace 添加fragment       beginTransaction.replace(R.id.lincontent, fr);       //提交       beginTransaction.commit();   }}

分别不同Fragment类代码:

Fragmnet1

package com.example.fragmenttest10;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 void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View v=inflater.inflate(R.layout.f1, null);        return v;    }}

Fragmnet2

package com.example.fragmenttest10;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 void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View v=inflater.inflate(R.layout.f2, null);        return v;    }}

Fragmnet3

package com.example.fragmenttest10;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 void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        View v=inflater.inflate(R.layout.f3, null);        return v;    }}

XML布局:

主布局代码:

<RelativeLayout 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" >    <LinearLayout        android:id="@+id/linbottom"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="weixinclick"            android:text="微信" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="findclick"            android:text="发现" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="meclick"            android:text="我" />    </LinearLayout>    <LinearLayout        android:id="@+id/lincontent"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@id/linbottom"        android:orientation="vertical" >    </LinearLayout></RelativeLayout>

分别对应代码:

f1.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" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是微信页面" /></LinearLayout>

f2.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" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是发现页面" /></LinearLayout>

f3.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" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="这是个人页面" /></LinearLayout>
0 0
原创粉丝点击