利用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;
}
}
}
相关文章推荐
- 利用ImageView + fragment 实现类似QQ消息界面的 点击不同按钮 展示不同界面
- 实现类似QQ、微信聊天界面,标题栏固定,键盘不遮挡底部输入框
- iOS 类似微信,QQ聊天界面的气泡聊天简单实现Demo
- Android应用实现点击back键时,使应用返回桌面,而不是退出应用,类似微信,qq等聊天软件
- ExpandableListView实现的简单类似QQ好友界面的可展开下拉列表
- android expandablelistview--实现类似qq界面的效果
- 下拉实现头部图片放大效果,实现类似QQ,新浪个人中心界面
- android 自定义listview实现仿微信/QQ设置界面的开发
- 利用Register protocol实现网页调用桌面程序(类似迅雷、QQ等)
- 利用 ViewPage 实现类似TabHost的功能,仅点击才可切换屏
- Android应用中使用ViewPager实现类似QQ的界面切换效果
- 利用Register protocol实现网页调用桌面程序(类似迅雷、QQ等)
- android 实现类似微信滑动切换界面
- 利用scrollview 实现类似微信,微博中查看多张图片,实现缩放拉伸
- swift uitableview 中实现分组,将cell分组显示,类似微信的好友界面
- 利用html5实现类似微信的手机摇一摇功能-计算摇动次数
- 利用SwipeRefreshLayout实现类似知乎客户端的一打开界面就自动刷新的效果
- 使用 electron 实现类似新版 QQ 的登录界面效果(阴影、背景动画、窗体3D翻转)
- 滑动菜单解决方案之三:使用TabHost控件实现微信界面的底部菜单效果
- Android QQ、微信聊天消息界面设计原理与实现