Android studio 中的TabWidget
1、TabWidget 的 layout文件
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/background"> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout> <TextView
android:id="@+id/intervalText1"
android:layout_width="match_parent"
android:layout_height="@dimen/interval"
android:background="@color/hint_title_background"
android:layout_above="@android:id/tabs"/> <TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="@dimen/tabs_height"
android:background="@color/white"
android:orientation="horizontal">
</TabWidget> </LinearLayout>
</TabHost>
2、tab布局的layout文件
<?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:gravity="center"
android:layout_marginTop="@dimen/tabs_interval"
android:orientation="vertical"
android:background="@color/white">
<ImageView
android:id="@+id/tab_icon"
android:layout_width="@dimen/tabs"
android:layout_height="@dimen/tabs"
android:scaleType="fitCenter"/>
<TextView
android:id="@+id/tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/tabs_interval"
android:textColor="@drawable/main_tab_textcolor"
android:textSize="@dimen/text_size3"/> </LinearLayout>
3、MainActivity
public class MainActivity extends TabActivity {
private static final String TAB_SALE = "SALE";
private static final String TAB_CART = "CART";
private static final String TAB_REPORT = "REPORT";
private static final String TAB_SETUP = "SETUP";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maintabs);
TabHost tabHost = getTabHost();
//first tab
tabHost.addTab(tabHost.newTabSpec(TAB_SALE)
.setIndicator(prepareTabView(TAB_SALE))
.setContent(new Intent(this, SaleActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
//second tab
tabHost.addTab(tabHost.newTabSpec(TAB_CART)
.setIndicator(prepareTabView(TAB_CART))
.setContent(new Intent(this, CartActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
//third tab
tabHost.addTab(tabHost.newTabSpec(TAB_REPORT)
.setIndicator(prepareTabView(TAB_REPORT))
.setContent(new Intent(this, ReportActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
//forth tab
tabHost.addTab(tabHost.newTabSpec(TAB_SETUP)
.setIndicator(prepareTabView(TAB_SETUP))
.setContent(new Intent(this, SetupActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
tabHost.setCurrentTab(0);//设置当前的选项卡,这里为Tab1
}
//自定义 标签按钮
private View prepareTabView(String text) {
View view = LayoutInflater.from(this).inflate(R.layout.main_tab_view, null);
ImageView imageView = (ImageView) view.findViewById(R.id.tab_icon);
imageView.setBackground(getDrawable(text));
TextView textView = (TextView) view.findViewById(R.id.tab_text);
textView.setText(text);
return view;
}
private Drawable getDrawable(String tabLabel){
Drawable backgroundDrawable = null;
if (tabLabel.equals(TAB_SALE)) {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_sale);
} else if (tabLabel.equals(TAB_CART)) {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_cart);
} else if (tabLabel.equals(TAB_REPORT)) {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_report);
} else {
backgroundDrawable = getResources().getDrawable(R.drawable.tab_setup);
}
return backgroundDrawable;
}
}
4、tab切换时图标改变
由于四个tab切换时实现图标改变的.xml文件相似,只列出其中一个。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/tab_sale_h"></item>
<item android:state_selected="false" android:drawable="@drawable/tab_sale_n"></item>
</selector>
Android studio 中的TabWidget的更多相关文章
- android studio 中移除module和恢复module
一.移除Android Studio中module 在Android Studio中想要删除某个module时,在Android Studio中选中module,右键发现没有delete,如图: An ...
- Android Studio中Button等控件的Text中字符串默认大写的解决方法
初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...
- .Net程序员之不学Java做安卓开发:Android Studio中的即时调试窗口
对学.Net的人来说,JAVA开发是一场噩梦. .net中的即时窗口,调试时直接在里面写代码,对程序中的各种方法/属性进行调用,很方便. Android Studio中找了好久,参考如下网址,也有类似 ...
- 如何将Eclipse中的项目迁移到Android Studio 中
如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...
- Android开发的小技巧,在Android Studio中使用Designtime Layout Attributes
在编写xml文件时,为了预览效果,经常会使用默认填上一些内容,比如TextView时,随便写上一个text <TextView ... android:text="Name:" ...
- 在android studio 中使用applicationid的问题
现在我需要对项目app的某个功能做性能测试,主要测试耗电量的多少. 1.我想到的方式是,我需要在同一台手机测试,同一个应用,需要安装在手机两次,第二次安装不覆盖第一次的安装. 在android stu ...
- Android studio 中的配置编译错误总结
1.编译Andorid 工程的时候,有时候出现gradle 报下面的错误. Error:(1, 0) Cause: com/android/build/gradle/LibraryPlugin : U ...
- Android Studio中清单文件改versionCode和versionName没效果的原因
在Android Studio中,项目的versionCode 和versionName 的控制不是在AndroidManifest.xml清单文件中更改的,而是在项目的build.gradle中更改 ...
- android studio中如何设置注释模板
在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android studio中该如何设置呢? 工具/原料 andro ...
随机推荐
- android 开发 View _11_ xml动画
请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/harvic880925/article/details/39996643 谢谢! 一.概述 Android的anima ...
- 安装grid时找不到ASM共享磁盘
1.安装ORACLE数据库集群软件grid时找不到共享磁盘,如下图: 2.网上找过有各种说法,但此处小编的解决方案是:通过重新安装软件:oracleasmlib-2.0.4-1.el6.x86_64. ...
- Laravel数据库操作 Eloquent ORM
模型首先在App目录下建立student的文件夹 里面放上 Student.php 且需要继承基类Model //允许批量赋值的字段// protected $fillable = ['name',' ...
- 无法启动 nexus 服务,错误1067:进程意外终止。java环境变量设置技巧。
Nexus启动失败 wrapper.log记载: 无支持版本 51.0,版本51.0指的是Java1.7. 分析: nexus版本为2.14.8,适用JRE版本为1.7. 已配置JAVA_HOME为1 ...
- hdu5698瞬间移动-(杨辉三角+组合数+乘法逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- PXC 搭建高可用集群
(1).PXC集群注意事项 1.PXC集群只支持innodb引擎 2.
- 2018年秋季学期面向对象程序设计(JAVA)课程总结
2018年秋季学期面向对象程序设计(JAVA)课程总结 时值2018年年末,按惯例对本学期教学工作小结如下: 1. 教学资源与教学辅助平台 教材:凯 S.霍斯特曼 (Cay S. Horstmann) ...
- Pandas字符串操作及实例应用
字符串操作 字符串对象方法 val = 'a,b, guido' val.split(',') ['a', 'b', ' guido'] pieces = [x.strip() for x in va ...
- oracle 表空间创建和删除
oracle数据库:数据库对象以及表数据都存储在表空间中,创建用户时可以指定对应的表空间.这样用户可以在各自的表空间中操作数据,互不干扰. 1. 表空间创建 若不清楚表空间对应文件的路径,可以登录系统 ...
- Python基础-python数据类型之集合(四)
集合 集合是一个无序的,不重复的数据组合,基本功能包括关系测试和消除重复元素. 集合对象还支持 union,intersection,difference和 sysmmetric difference ...