Android:布局实例之模仿微信Tab
微信Tab预览效果:

思路:
1、用TabHost+RadioGroup搭建基本布局,以RadioGroup代替TabWidget
2、设置按钮和文字的的样式和selector
3、创建相应的Activity
4、实现按钮和内容切换
布局:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/tabhost"
> <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"
></FrameLayout> <!-- 隐藏TabWidget -->
<TabWidget
android:id="@android:id/tabs"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</TabWidget> <!-- 视觉上,用单选按钮替代TabWidget -->
<RadioGroup
android:id="@+id/main_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/mmfooter_bg"
android:paddingTop="8dp"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/tab_icon_weixin"
android:checked="true"
android:textColor="@color/tab_text"
android:drawableTop="@drawable/tab_weixin"
android:text="@string/radio1_text"
style="@style/tab_button_bg"
/> <RadioButton
android:id="@+id/tab_icon_address"
android:textColor="@color/tab_text"
android:drawableTop="@drawable/tab_address"
android:text="@string/radio2_text"
style="@style/tab_button_bg"
/> <RadioButton
android:id="@+id/tab_icon_friend"
android:textColor="@color/tab_text"
android:drawableTop="@drawable/tab_frd"
android:text="@string/radio3_text"
style="@style/tab_button_bg"
/> <RadioButton
android:id="@+id/tab_icon_setting"
android:textColor="@color/tab_text"
android:drawableTop="@drawable/tab_set"
android:text="@string/radio4_text"
style="@style/tab_button_bg"
/> </RadioGroup>
</LinearLayout>
</TabHost>
按钮样式:
<!-- 按钮文字样式 -->
<style name="tab_text_color">
<item name="android:textColor">@color/tab_text</item>
<item name="android:textSize">12dp</item>
<!-- 从左向右跑马灯效果 -->
<item name="android:ellipsize">marquee</item>
<!-- 单行显示 -->
<item name="android:singleLine">true</item>
</style> <!-- 按钮样式 -->
<style name="tab_button_bg">
<item name="android:textAppearance">@style/tab_text_color</item>
<item name="android:gravity">center</item>
<item name="android:background">@drawable/tab_bg</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:layout_weight">1</item>
</style>
按钮图片selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:state_checked="true" android:drawable="@drawable/tab_address_pressed"></item>
<item android:drawable="@drawable/tab_address_normal"></item>
</selector>
按钮文字selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/white"></item>
<item android:color="@color/grey"></item>
</selector>
程序:
public class MainActivity extends TabActivity{
private TabHost tabhost;
private RadioGroup main_radiogroup;
private RadioButton tab_icon_weixin, tab_icon_address, tab_icon_friend,tab_icon_setting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
//获取按钮
main_radiogroup = (RadioGroup) findViewById(R.id.main_radiogroup);
tab_icon_weixin = (RadioButton) findViewById(R.id.tab_icon_weixin);
tab_icon_address = (RadioButton) findViewById(R.id.tab_icon_address);
tab_icon_friend = (RadioButton) findViewById(R.id.tab_icon_friend);
tab_icon_setting = (RadioButton) findViewById(R.id.tab_icon_setting);
//往TabWidget添加Tab
tabhost = getTabHost();
tabhost.addTab(tabhost.newTabSpec("tag1").setIndicator("0").setContent(new Intent(this,Activity1.class)));
tabhost.addTab(tabhost.newTabSpec("tag2").setIndicator("1").setContent(new Intent(this,Activity2.class)));
tabhost.addTab(tabhost.newTabSpec("tag3").setIndicator("2").setContent(new Intent(this,Activity3.class)));
tabhost.addTab(tabhost.newTabSpec("tag4").setIndicator("3").setContent(new Intent(this,Activity4.class)));
//设置监听事件
checkListener checkradio = new checkListener();
main_radiogroup.setOnCheckedChangeListener(checkradio);
}
//监听类
public class checkListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//setCurrentTab 通过标签索引设置当前显示的内容
//setCurrentTabByTag 通过标签名设置当前显示的内容
switch(checkedId){
case R.id.tab_icon_weixin:
tabhost.setCurrentTab(0);
//或
//tabhost.setCurrentTabByTag("tag1");
break;
case R.id.tab_icon_address:
tabhost.setCurrentTab(1);
break;
case R.id.tab_icon_friend:
tabhost.setCurrentTab(2);
break;
case R.id.tab_icon_setting:
tabhost.setCurrentTab(3);
break;
}
}
}
}
相关的文章:
Android:布局实例之模仿微信Tab的更多相关文章
- [转]Android:布局实例之模仿QQ登录界面
Android:布局实例之模仿QQ登录界面 预览图: 准备: 1.找到模仿对象 QQ登陆界面UI下载>>>>> 2.导入工程 3.查看布局结构和使用控件 其对应效果图分布 ...
- Android:布局实例之模仿QQ登录界面
预览图: 准备: 1.找到模仿对象 QQ登陆界面UI下载>>>>> 2.导入工程 3.查看布局结构和使用控件 其对应效果图分布为 4.分析样式选择器 下拉箭头2种样式:点 ...
- Android:布局实例之模仿京东登录界面
预览图及布局结构参考: 布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout ...
- Android Studio精彩案例(三)《模仿微信ViewPage+Fragment实现方式二》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 写在前面的话:此专栏是博主在工作之余所写,每一篇文章尽可能写的思路清晰一些,属于博主的"精华"部分,不同于以往专栏 ...
- android本地数据库,微信数据库WCDB for Android 使用实例
android本地数据库,微信数据库WCDB for Android 使用实例 Home · Tencent/wcdb Wikihttps://github.com/Tencent/wcdb/wiki ...
- android actionbar viewpager 实现类微信主界面布局
1 Activity public class MainActivity extends FragmentActivity { private ViewPager pager; private Act ...
- 自定义控件(模仿微信ToggleButton控件)
弄过android开发的都知道,系统有一个默认的ToggleButton,但很多人都觉得他很难看,当然也包括我.如果你感觉他不难看,那你就继续使用系统的吧,这篇文章对你来说是多余的了. 今天来写一个模 ...
- [Android] Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单
Android 使用 FragmentTabHost + Fragment 实现 微信 底部菜单 利用FragmentTabHost实现底部菜单,在该底部菜单中,包括了4个TabSpec,每个TabS ...
- 【转】 Android常用实例—Alert Dialog的使用
Android常用实例—Alert Dialog的使用 AlertDialog的使用很普遍,在应用中当你想要用户做出“是”或“否”或者其它各式各样的选择时,为了保持在同样的Activity和不改变用户 ...
随机推荐
- linux下安装subversion出现libtool: link: only absolute run-paths are allowed
configure时加上 --with-apache-libexecdir=/usr/local/apache
- 理解OAuth 2.0 -摘自网络
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OA ...
- JS认证Exchange
function ExchangeLogin() { vstrServer='<%=LocationUrl %>' vstrDomain = '<%=userLogin.AD %&g ...
- 深入.Net字符串类型
.Net的字符串其实还是有很多东西可以写的.但是最近在学习SQL Server,只好先做下最近学习到的一些巧用,妙用之类的东西. 巧用String.Join拼接字串数组,字符串集合为字符串.如果在之前 ...
- RESTful API 设计最佳实践(转)
摘要:目前互联网上充斥着大量的关于RESTful API(为了方便,以后API和RESTful API 一个意思)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API格式如何?你的API ...
- c++面试题总结(1)
1.int a=5,则 ++(a++)的值是() A.5 B. 6 C.7 D.逻辑错误 a++返回的是一个临时变量,这里是右值,不能再前面++了 2.下面 ...
- cocos2d-x 2.2.5 安卓工程编译的问题
原址:http://www.cocoachina.com/bbs/read.php?tid=217124 新的cocos2d-x 2.2.5 在使用Eclipse的安卓NDK 9 的编译器进行编译的时 ...
- jfinal的ajax例子
@(编程) 简介 JFinal 是基于 Java 语言的极速 WEB + ORM 框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restful. 在拥有Java语言所有 ...
- HDU 5828 Rikka with Sequence (线段树+剪枝优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...