先看看QQ的效果(当然,本文的例子比较简单):
“好友”、“群”、“多人聊天”等在一个Activity里面,但是点击时可以切换成不同的界面!
思路:在一个Activity里面,有两个按钮,点击两个按钮时切换到不同的Fragment
Activity代码:
package com.example.demo3;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button todayNews,everNews;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化组件
todayNews=(Button) findViewById(R.id.todayNews);
everNews=(Button) findViewById(R.id.everNews);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fl_content, new TodayNewsFragment());
fragmentTransaction.commit();
//当点击往日新闻的按钮时,触发该事件
everNews.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
everNews.setTextColor(getResources().getColor(R.color.tab));
todayNews.setTextColor(Color.WHITE);
everNews.setBackgroundResource(R.drawable.left_bold);
todayNews.setBackgroundResource(R.drawable.right_transparent);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl_content, new EverNewsFragment());
fragmentTransaction.commit();
}
});
//当点击今日新闻的按钮时,触发该事件
todayNews.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
todayNews.setTextColor(getResources().getColor(R.color.tab));
everNews.setTextColor(Color.WHITE);
todayNews.setBackgroundResource(R.drawable.left_bold);
everNews.setBackgroundResource(R.drawable.right_transparent);
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl_content, new TodayNewsFragment());
fragmentTransaction.commit();
}
});
}
}
main.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:background="@android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/tab"
android:gravity="center"
android:paddingLeft="8dp" >
<Button
android:id="@+id/todayNews"
android:layout_width="90dp"
android:layout_height="25dp"
android:background="@drawable/left_bold"
android:gravity="center"
android:padding="2dp"
android:text="今日新闻"
android:textColor="@color/tab"
android:textSize="14dp" />
<Button
android:id="@+id/everNews"
android:layout_width="90dp"
android:layout_height="25dp"
android:background="@drawable/right_transparent"
android:gravity="center"
android:padding="2dp"
android:text="往日新闻"
android:textColor="@color/white"
android:textSize="14dp" />
</LinearLayout>
<FrameLayout
android:id="@+id/fl_content"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp" />
</LinearLayout>
两个Fragment:
package com.example.demo3;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TodayNewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.todaynewsfragment, container, false);
}
}
package com.example.demo3;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class EverNewsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.evernewsfragment, container, false);
}
}
两个Fragment对应的xml里面都只有一个TextView...
效果:
源码下载:http://download.csdn.net/download/fancheng614/9970586