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和不改变用户 ...
随机推荐
- Android Activity学习笔记(一)
Activity为Android应用的四大组件之一,提供界面来与用户完成交互等操作.其中Activity的生命周期的知识这里做个笔记. Activity的生命周期由以下几个部分组成: 1.onCrea ...
- gpgcheck
warning: rpmts_HdrFromFdno: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Public key for co ...
- 前端复习-02-ajax原生以及jq和跨域方面的应用。
ajax这块我以前一直都是用现成的jq封装好的东西,而且并没有深入浅出的研究过,也没有使用过原生形式的实现.包括了解jsonp和跨域的相关概念但是依然没有实现过,其中有一个重要的原因我认为是我当时并不 ...
- C#修改下拉框选项的高度
重写ListBox.DrawItem事件处理,别忘记将ListBox.DrawMode 设置为OwnerDrawVariable,ListBox.ItemHeight值改大一点,字体也适当放大一号. ...
- Operation not applicable
ClientDataSet.DataSetProvider1 Operation not applicable ClientDataSet1->Open(); 解决办法 1.update new ...
- Configure the Struts Tag Libraries
In Struts framework, you always need to configure the Struts tag libraries in order to access it in ...
- c#读properties文件
@(编程) properties文件 MONGO_URL = mongodb://172.16.20.3/srtc_dc CURRENT_VERSION = 2.0 IS_AUTO_UPDATE = ...
- sql的join用法
SQL join 用于把来自两个或多个表的行结合起来,sql join主要包括inner join. left join .right join .full outer join. 先介绍一下表里面的 ...
- Dell商用台式机、笔记本、服务器800电话
戴尔Optiplex商用台式机 售后服务电话 800-858-0950 选1选2选2 戴尔Latitude商用笔记本 售后服务电话 800-858-0950 选1选3选2 戴尔服务器PowerEdge ...
- CodeForces 709A Juicer (水题, 模拟)
题意:给定 n 个桔子的大小,一个杯子的容积,一个最大限度,挨着挤桔子汁,如果大小大于限度,扔掉,如果不杯子满了倒掉,问你要倒掉多少杯. 析:直接按要求模拟就好,满了就清空杯子. 代码如下: #pra ...