您的位置:首页 > 移动开发 > 微信开发

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

2014-01-15 09:48 399 查看
<?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;
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐