类似QQ中“好友”、“群”、“多人聊天”之间在一个Activity里面切换界面

先看看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


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值