界面切換gragment

效果圖: java

佈局: android

<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"
    tools:context=".MainActivity" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
		android:orientation="horizontal" >
        <TextView 
            android:id="@+id/tab1"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="社會新聞"/>
         <TextView 
             android:id="@+id/tab2"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="體育新聞"/>
          <TextView 
              android:id="@+id/tab3"
            android:gravity="center"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="娛樂新聞"/>
           <TextView 
               android:id="@+id/tab4"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="軍事新聞"/>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
		android:orientation="horizontal" >
           </LinearLayout>
</LinearLayout>



fragment佈局:(也能夠在代碼中進行佈局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frg1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    
	<TextView
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="社會"
	    />
</LinearLayout>



fragment1.java:
package com.pas.changefrg;

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.frg1, null);
    }
}



MainActivity:
package com.pas.changefrg;

import android.os.Bundle;
import android.app.Activity;
//注意包名 使用支持包能夠更好的向下兼容
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private LinearLayout content;
    private FragmentManager fm;
    private FragmentTransaction ft;
    private TextView tv1, tv2, tv3, tv4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	content = (LinearLayout) findViewById(R.id.ll_content);
	tv1 = (TextView) findViewById(R.id.tab1);
	tv2 = (TextView) findViewById(R.id.tab2);
	tv3 = (TextView) findViewById(R.id.tab3);
	tv4 = (TextView) findViewById(R.id.tab4);
	tv1.setOnClickListener(this);
	tv2.setOnClickListener(this);
	tv3.setOnClickListener(this);
	tv4.setOnClickListener(this);

	fm = getSupportFragmentManager();
	ft = fm.beginTransaction();
	ft.replace(R.id.ll_content, new Fragment1());
	ft.commit();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
	// Inflate the menu; this adds items to the action bar if it is present.
	getMenuInflater().inflate(R.menu.main, menu);
	return true;
    }

    @Override
    public void onClick(View v) {
	ft = fm.beginTransaction();
	switch (v.getId()) {
	case R.id.tab1:
	    ft.replace(R.id.ll_content, new Fragment1());
	    break;
	case R.id.tab2:
	    ft.replace(R.id.ll_content, new Fragment2());
	    break;
	case R.id.tab3:
	    ft.replace(R.id.ll_content, new Fragment3());
	    break;
	case R.id.tab4:
	    ft.replace(R.id.ll_content, new Fragment4());
	    break;
	default:
	    break;
	}

	ft.commit();
    }

}
相關文章
相關標籤/搜索