七:主界面用TabHost分爲單聊和羣聊

        登陸和註冊都寫完了,現在要進入主界面,接着前面的寫,主界面用TabHost分爲單聊和羣聊,本人認爲寫過Android的,都應該用過吧,本來不想貼出來的,想了想,就兩行代碼,貼一下吧。

start:

1、先看一下主界面的效果,自己截的兩張圖片,哈哈,比較醜。



2、主界面的佈局代碼:activity_maintab.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <RadioGroup
            android:id="@+id/rg_maintab_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="bottom"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rb_maintab_linkman"
                style="@style/MTabButton"
                android:checked="true"
                android:background="@drawable/maintab_linkman_bg"
                />

            <RadioButton
                android:id="@+id/rb_maintab_hostroom"
                style="@style/MTabButton"
                android:background="@drawable/maintab_hostroom_bg"
                 />
        </RadioGroup>

    </LinearLayout>

</TabHost>


3、RadioButton的公共樣式:

<style name="MTabButton">
        <item name="android:gravity">center_horizontal</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">60dp</item>
        <item name="android:button">@null</item>
        <item name="android:layout_weight">1.0</item>
        <item name="android:paddingBottom">2.0dip</item>
        <item name="android:paddingTop">2.0dip</item>
    </style>

4、RadiaButton的聯繫人背景,和會議室背景的代碼一樣,我就不貼了。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item android:drawable="@drawable/linkman_up" android:state_checked="false"/>
    <item android:drawable="@drawable/linkman_down" android:state_checked="true"/>

</selector>


5、MainTabActivity.java類:

package org.hkby.lwx.activity;

import org.hkby.lwx.common.XmppTool;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;

/**
 * main tabhost
 * 
 * @author liaowuxing
 */
@SuppressWarnings("deprecation")
public class MainTabActivity extends TabActivity {
	/**
	 * Called when the activity is first created.
	 */
	private TabHost tabHost;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_maintab);
		initView();
	}

	public void initView() {
		tabHost = this.getTabHost();
		TabHost.TabSpec tabSpec;
		Intent intent;

		intent = new Intent().setClass(this, LinkmanActivity.class);
		tabSpec = tabHost.newTabSpec("linkman").setIndicator("linkman")
				.setContent(intent);
		tabHost.addTab(tabSpec);

		intent = new Intent().setClass(this, HostRoomActivity.class);
		tabSpec = tabHost.newTabSpec("hostroom").setIndicator("hostroom")
				.setContent(intent);
		tabHost.addTab(tabSpec);
		tabHost.setCurrentTab(0);

		RadioGroup radioGroup = (RadioGroup) this
				.findViewById(R.id.rg_maintab_group);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rb_maintab_linkman:
					tabHost.setCurrentTabByTag("linkman");
					break;
				case R.id.rb_maintab_hostroom:
					tabHost.setCurrentTabByTag("hostroom");
					break;
				default:
					break;
				}
			}
		});
	}

	/**
	 * Return to cancel connection
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		if (XmppTool.getConnection() != null) {
			XmppTool.closeConnection();
		}
	}
}


6、項目結構:


7、ok,這一篇先爲寫聊天做了一下鋪墊。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章