利用tabhost实现类似QQ、微信界面

本文介绍了一种使用 Android 的 TabHost 控件实现标签页切换功能的方法。通过自定义 TabHost 的 TabSpec,可以灵活地设置标签的文字、图标及对应的内容页面。文章详细展示了如何初始化 Intent 和 TabSpec,以及如何监听 RadioGroup 的选择变化来切换当前显示的标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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">
    
<TabHost
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/tabhost">
  <LinearLayout android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
	<FrameLayout android:id="@android:id/tabcontent"
	android:layout_width="fill_parent" android:layout_height="0.0dip"
	android:layout_weight="1.0" >

		
	</FrameLayout>
	
		<TabWidget android:id="@android:id/tabs" android:visibility="gone"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:layout_weight="0.0" />
		
		<!--  android:background="@drawable/tabs_bg" -->
		<RadioGroup android:id="@+id/tab_radiogroup"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content" android:gravity="center_vertical"
		android:layout_gravity="bottom" android:orientation="horizontal">
		
		<!--  	android:drawableTop="@drawable/account01"  -->
			<RadioButton android:id="@+id/radio_button_session"
			android:layout_marginTop="2.0dip" android:checked="true"
			android:background="@drawable/chat" 
			style="@style/tab_bottom" 
			/>
			
			<!--  android:drawableTop="@drawable/account02" -->
			<RadioButton android:id="@+id/radio_button_friendlist"
			android:layout_marginTop="2.0dip"  
			style="@style/tab_bottom" android:background="@drawable/friends"
			  />
			
			<!-- android:drawableTop="@drawable/account03"-->
			<RadioButton android:id="@+id/radio_button_find"
			android:layout_marginTop="2.0dip"
			style="@style/tab_bottom" android:background="@drawable/discover"/>
				
			<!--android:drawableTop="@drawable/account04" -->
			<RadioButton android:id="@+id/radio_button_setting"
			android:layout_marginTop="2.0dip"
			style="@style/tab_bottom" android:background="@drawable/setting"/>
		</RadioGroup>

 </LinearLayout>

</TabHost>
</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">
    
<TabHost
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/tabhost">
  <LinearLayout android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	
	<FrameLayout android:id="@android:id/tabcontent"
	android:layout_width="fill_parent" android:layout_height="0.0dip"
	android:layout_weight="1.0" >

		
	</FrameLayout>
	
		<TabWidget android:id="@android:id/tabs" android:visibility="gone"
		android:layout_width="fill_parent" android:layout_height="wrap_content"
		android:layout_weight="0.0" />
		
		<!--  android:background="@drawable/tabs_bg" -->
		<RadioGroup android:id="@+id/tab_radiogroup"
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content" android:gravity="center_vertical"
		android:layout_gravity="bottom" android:orientation="horizontal">
		
		<!--  	android:drawableTop="@drawable/account01"  -->
			<RadioButton android:id="@+id/radio_button_session"
			android:layout_marginTop="2.0dip" android:checked="true"
			android:background="@drawable/chat" 
			style="@style/tab_bottom" 
			/>
			
			<!--  android:drawableTop="@drawable/account02" -->
			<RadioButton android:id="@+id/radio_button_friendlist"
			android:layout_marginTop="2.0dip"  
			style="@style/tab_bottom" android:background="@drawable/friends"
			  />
			
			<!-- android:drawableTop="@drawable/account03"-->
			<RadioButton android:id="@+id/radio_button_find"
			android:layout_marginTop="2.0dip"
			style="@style/tab_bottom" android:background="@drawable/discover"/>
				
			<!--android:drawableTop="@drawable/account04" -->
			<RadioButton android:id="@+id/radio_button_setting"
			android:layout_marginTop="2.0dip"
			style="@style/tab_bottom" android:background="@drawable/setting"/>
		</RadioGroup>

 </LinearLayout>

</TabHost>
</LinearLayout>


public class ActTabHost extends Activity implements OnCheckedChangeListener {
	private final String TAG = "ActTabHost";
	private TabHost tabHost;
	private Intent sessionIntent; // 会话
	private Intent friendsListIntent; // 好友
	private Intent findingIntent; // 发现
	private Intent settingIntent; // 设置

	private Thread mThread;

	private ServiceConnection mServiceConnection = null;
	private SocketCommunicationService myService = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabhost);
		tabHost = (TabHost) findViewById(R.id.tabhost);
		LocalActivityManager groupActivity = new LocalActivityManager(this,
				false);

		groupActivity.dispatchCreate(savedInstanceState);

		tabHost.setup(groupActivity);

		// 绑定服务并启动接收消息的服务
		bindMyReceiveMsgService();

		// if(mThread == null) {
		// mThread = new Thread(loadData);
		// mThread.start();//线程启动
		// }

		initIntent();

		addSpec();

		((RadioGroup) findViewById(R.id.tab_radiogroup))
				.setOnCheckedChangeListener(this);

	};

	private Handler mHandler = new Handler() {
		// message对象中包含了线程方法发过来的数据
		public void handleMessage(Message msg) {

		}
	};

	// 这是一个被后台线程执行的方法,由bnt2的click事件 创建线程并启动
	Runnable loadData = new Runnable() {
		public void run() {
			LoadServerData loaddata = new LoadServerData();
			if (loaddata.loadFriendsList(Controller.getUserId(), 1))
				CommonFunctions.generateLogV("100", "从服务器加载好友列表成功!");
			else
				CommonFunctions.generateLogV("100", "从服务器加载好友列表失败!");
			// 从本地数据库取数据
			LocalDataProcess localDataProcess = new LocalDataProcess();
			localDataProcess.downLoadLocalData();
			CommonFunctions.generateLogV("100", "从本地数据库加载数据完成!");
		}
	};

	/**
	 * 初始化各个tab标签对应的intent
	 */
	private void initIntent() {
		sessionIntent = new Intent(this, ActSession.class);
		friendsListIntent = new Intent(this, ActFriendsList.class);
		findingIntent = new Intent(this, ActFinding.class);
		settingIntent = new Intent(this, ActSetting.class);
	}

	/**
	 * 为tabHost添加各个标签项
	 */
	private void addSpec() {
		tabHost.addTab(this.buildTagSpec("tab_session", "会话1", R.drawable.chat,
				sessionIntent));
		tabHost.addTab(this.buildTagSpec("tab_friendlist", "好友1",
				R.drawable.friends, friendsListIntent));
		tabHost.addTab(this.buildTagSpec("tab_finding", "发现1",
				R.drawable.discover, findingIntent));
		tabHost.addTab(this.buildTagSpec("tab_setting", "设置1",
				R.drawable.setting, settingIntent));
	}

	/**
	 * 自定义创建标签项的方法
	 * 
	 * @param tagName
	 *            标签标识
	 * @param tagLable
	 *            标签文字
	 * @param icon
	 *            标签图标
	 * @param content
	 *            标签对应的内容
	 * @return
	 */

	private TabHost.TabSpec buildTagSpec(String tagName, String tagLable,
			int icon, Intent content) {
		return tabHost.newTabSpec(tagName)
				.setIndicator(tagLable, getResources().getDrawable(icon))
				.setContent(content);
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		switch (checkedId) {

		case R.id.radio_button_session:

			tabHost.setCurrentTabByTag("tab_session");
			Controller.setCurrentActivity(ActSession.TAG);
			break;

		case R.id.radio_button_friendlist:

			tabHost.setCurrentTabByTag("tab_friendlist");
			Controller.setCurrentActivity(ActFriendsList.TAG);

			break;
		case R.id.radio_button_find:

			tabHost.setCurrentTabByTag("tab_finding");
			Controller.setCurrentActivity(ActFinding.TAG);
			break;

		case R.id.radio_button_setting:

			tabHost.setCurrentTabByTag("tab_setting");
			Controller.setCurrentActivity(ActSetting.TAG);
			break;
		}
	}

	public void bindMyReceiveMsgService() {
		mServiceConnection = new ServiceConnection() {
			@Override
			public void onServiceConnected(ComponentName name, IBinder service) {
				// TODO Auto-generated method stub
				LocalBinder binder = (LocalBinder) service;
				myService = binder.getService();
				myService.startReceiveMsgThread();
			}

			@Override
			public void onServiceDisconnected(ComponentName name) {
				// TODO Auto-generated method stub
				CommonFunctions.generateLogV(TAG, "onServiceDisconnected");
			}
		};
		Intent intent = new Intent(ActTabHost.this,
				SocketCommunicationService.class);
		if (false == bindService(intent, mServiceConnection,
				Context.BIND_AUTO_CREATE)) {
			CommonFunctions.showMessage(ActTabHost.this, "接收消息服务绑定失败!");
			return;
		}
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值