仿微信主页的切换
2015-09-05 20:54
507 查看
0.效果
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905202838529-1374322110.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905203010592-492949255.png)
1.创建首页的Activity
创建首页Activity的布局页面
2.创建每个Tab显示的自定义View,在layout目录下新建tab_indicator.xml
创建自定义View对象
3. 创建几个展示区域的自定义Fragment
简单的布局
相关资源文件
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205200217-422086141.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205204764-2029239844.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205209436-2138920951.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205215248-2101719036.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205221342-1086980077.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205228576-403233490.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205234889-266872889.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205241186-2040413848.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205246811-232265619.png)
可能会遇到的错误:
java.lang.IllegalStateException: No tab content FrameLayout found for id xxxxx
解决:换个新的android-support-v4.jar,覆盖原来的
代码地址: https://github.com/cbooy/FragmentTabHostDemo
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905202838529-1374322110.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905203010592-492949255.png)
1.创建首页的Activity
public class MaintabActivity extends FragmentActivity implements OnTabChangeListener { private FragmentTabHost tabHost; //定义 每个Tab的 Tag --- start private final static String TAG_CHAT = "chat"; private final static String TAG_CONTACTS = "contacts"; private final static String TAG_DISCOVER = "discover"; private final static String TAG_ME = "me"; // 定义 每个Tab的 Tag --- end // 定义 每个Tab展示的View --- start private TabIndicator chatIndicator; private TabIndicator contactsIndicator; private TabIndicator discoverIndicator; private TabIndicator meIndicator; // 定义 每个Tab展示的View --- end @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initIndicator(); initTabHost(); } /** * 初始化 TabHost */ private void initTabHost() { // 1. 初始化TabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); tabHost.setup(this, getSupportFragmentManager(), R.id.homeTabContent); // 2.1 创建聊天TabSpec TabSpec chatTabSpec = tabHost.newTabSpec(TAG_CHAT); chatTabSpec.setIndicator(chatIndicator); // 2.2 创建联系人TabSpec TabSpec contactsTabSpec = tabHost.newTabSpec(TAG_CONTACTS); contactsTabSpec.setIndicator(contactsIndicator); // 2.3 创发现TabSpec TabSpec discoverTabSpec = tabHost.newTabSpec(TAG_DISCOVER); discoverTabSpec.setIndicator(discoverIndicator); // 2.4 创我TabSpec TabSpec meTabSpec = tabHost.newTabSpec(TAG_ME); meTabSpec.setIndicator(meIndicator); // 3. 添加 TabSpec tabHost.addTab(chatTabSpec,ChatFragment.class,null); tabHost.addTab(contactsTabSpec,ContactsFragment.class,null); tabHost.addTab(discoverTabSpec,DiscoverFragment.class,null); tabHost.addTab(meTabSpec,MeFragment.class,null); // 4.去掉 每个Tab后面的 竖线 tabHost.getTabWidget().setDividerDrawable(null); // 5. 设置初始化选中的Tab tabHost.setCurrentTabByTag(TAG_CHAT); chatIndicator.setCurrentFocus(true); // 6.给每个Tab添加监听器 tabHost.setOnTabChangedListener(this); } /** * 初始化每个需要展示的View */ private void initIndicator(){ chatIndicator = new TabIndicator(this); chatIndicator.setTabHit("微信"); chatIndicator.setTabIcon(R.drawable.tab_icon_chat_normal, R.drawable.tab_icon_chat_focus); contactsIndicator = new TabIndicator(this); contactsIndicator.setTabHit("通讯录"); contactsIndicator.setTabIcon(R.drawable.tab_icon_contact_normal, R.drawable.tab_icon_contact_focus); contactsIndicator.setUnread(1); discoverIndicator = new TabIndicator(this); discoverIndicator.setTabHit("发现"); discoverIndicator.setTabIcon(R.drawable.tab_icon_discover_normal, R.drawable.tab_icon_discover_focus); meIndicator = new TabIndicator(this); meIndicator.setTabHit("我"); meIndicator.setTabIcon(R.drawable.tab_icon_me_normal, R.drawable.tab_icon_me_focus); } /** * 每个Tab的点击监听 */ @Override public void onTabChanged(String tabId) { buildIndicator(); // 点击微信的Tab if(tabId.equals(TAG_CHAT)){ chatIndicator.setTabSelected(true); } // 点击联系人的Tab if(tabId.equals(TAG_CONTACTS)){ contactsIndicator.setTabSelected(true); } // 点击发现的Tab if(tabId.equals(TAG_DISCOVER)){ discoverIndicator.setTabSelected(true); } // 点击我的Tab if(tabId.equals(TAG_ME)){ meIndicator.setTabSelected(true); } } /** * 点击之后先将全部的设置为非点击状态 */ private void buildIndicator() { chatIndicator.setTabSelected(false); contactsIndicator.setTabSelected(false); discoverIndicator.setTabSelected(false); meIndicator.setTabSelected(false); } }
创建首页Activity的布局页面
<?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:orientation="vertical" > <FrameLayout android:id="@+id/homeTabContent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#FFF1F1F1" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
2.创建每个Tab显示的自定义View,在layout目录下新建tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="54dip" android:layout_marginLeft="-3dip" android:layout_marginRight="-3dip" android:layout_weight="1" > <LinearLayout android:id="@+id/ll_tab_info" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/tab_indicator_icon" android:layout_width="34dp" android:layout_height="34dp" android:contentDescription="@null" android:src="@drawable/tab_icon_me_normal" /> <TextView android:id="@+id/tab_indicator_hint" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aaaa" android:textSize="12sp" /> </LinearLayout> <TextView android:id="@+id/tab_indicator_unread" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/ll_tab_info" android:layout_marginRight="3dp" android:layout_marginTop="3dp" android:background="@drawable/action_unread_icon" android:gravity="center" android:text="1" android:textColor="#ffffffff" android:textSize="10sp" /> </RelativeLayout>
创建自定义View对象
/** * 创建自定义View * @author chenhao24 * */ public class TabIndicator extends RelativeLayout { private ImageView ivTabIcon; private TextView tvTabHint; private TextView tvTabUnRead; private int focusId = -1; private int normalId = -1; private int unreadCount = 0; public TabIndicator(Context context) { this(context, null); } public TabIndicator(Context context, AttributeSet attrs) { super(context, attrs); View.inflate(context, R.layout.tab_indicator, this); ivTabIcon = (ImageView) findViewById(R.id.tab_indicator_icon); tvTabHint = (TextView) findViewById(R.id.tab_indicator_hint); tvTabUnRead = (TextView) findViewById(R.id.tab_indicator_unread); setUnread(0); } public void setTabIcon(int normalId, int focusId) { this.normalId = normalId; this.focusId = focusId; } public void setTabHit(String text) { tvTabHint.setText(text); } public void setUnread(int unreadCount) { this.unreadCount = unreadCount; if (unreadCount <= 0) { tvTabUnRead.setVisibility(View.GONE); } else { if (unreadCount >= 100) { tvTabUnRead.setText("99+"); } else { tvTabUnRead.setText("" + unreadCount); } tvTabUnRead.setVisibility(View.VISIBLE); } } public int getUnreadCount() { return this.unreadCount; } public void setCurrentFocus(boolean current) { if (current) { if (focusId != -1) { ivTabIcon.setImageResource(focusId); } } else { if (normalId != -1) { ivTabIcon.setImageResource(normalId); } } } // 设置选中 public void setTabSelected(boolean selected) { if (selected) { ivTabIcon.setImageResource(focusId); } else { ivTabIcon.setImageResource(normalId); } } }
3. 创建几个展示区域的自定义Fragment
public class ChatFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.show_info, container,false); TextView tvInfo = (TextView) view.findViewById(R.id.tv_info); tvInfo.setText("这里显示微信的列表"); return view; } }
public class ContactsFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.show_info, container,false); TextView tvInfo = (TextView) view.findViewById(R.id.tv_info); tvInfo.setText("这里显示联系人的列表"); return view; } }
public class DiscoverFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.show_info, container,false); TextView tvInfo = (TextView) view.findViewById(R.id.tv_info); tvInfo.setText("这里显示发现的列表"); return view; } }
public class MeFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.show_info, container,false); TextView tvInfo = (TextView) view.findViewById(R.id.tv_info); tvInfo.setText("这里显示我的列表"); return view; } }
简单的布局
<?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:layout_gravity="center" android:orientation="vertical" > <TextView android:id="@+id/tv_info" android:layout_width="match_parent" android:layout_height="match_parent" android:text="内容显示区域" /> </LinearLayout>
相关资源文件
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205200217-422086141.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205204764-2029239844.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205209436-2138920951.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205215248-2101719036.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205221342-1086980077.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205228576-403233490.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205234889-266872889.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205241186-2040413848.png)
![](http://images2015.cnblogs.com/blog/406443/201509/406443-20150905205246811-232265619.png)
可能会遇到的错误:
java.lang.IllegalStateException: No tab content FrameLayout found for id xxxxx
解决:换个新的android-support-v4.jar,覆盖原来的
代码地址: https://github.com/cbooy/FragmentTabHostDemo
相关文章推荐
- 微信打飞机小游戏的尝试
- 微信抢红包微信 PHP代码实现
- ActionScript 3.0 学习(十) AS3 模拟转盘抽奖小程序
- java日历小程序
- 做微信运营 不可不知的内容编辑绝技
- 个人屌丝微信创业 怎么入手吸粉转化
- 个人屌丝微信创业 怎么入手吸粉转化
- 浅谈Android中微信的文本,音频,视频,链接,表情,图片分享接口开发详解
- 三级分销
- 成为微信公众号开发者
- 第三方应用分享到微信朋友圈功能
- 我的微信头像换成国旗后的遭遇
- 微信公众平台开发简单介绍
- 获取微信用户信息出现乱码
- 平时编些小程序
- 微信支付 php jsapi 配置调试
- android---仿微信主界面设计
- 基于第三方微信授权登录的iOS代码分析
- 开源分享 一(StickerCamera + 仿微信多图选择)
- [置顶] 微信sdk应用消息的接入(android)