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的更多相关文章

  1. android studio 中移除module和恢复module

    一.移除Android Studio中module 在Android Studio中想要删除某个module时,在Android Studio中选中module,右键发现没有delete,如图: An ...

  2. Android Studio中Button等控件的Text中字符串默认大写的解决方法

    初学Android的时候,在Android Studio中xml里面添加一个Button.EditText等控件后,它的Text总是会显示大写,即使你输入的字符串是小写也不行,控制字符串大小写的属性是 ...

  3. .Net程序员之不学Java做安卓开发:Android Studio中的即时调试窗口

    对学.Net的人来说,JAVA开发是一场噩梦. .net中的即时窗口,调试时直接在里面写代码,对程序中的各种方法/属性进行调用,很方便. Android Studio中找了好久,参考如下网址,也有类似 ...

  4. 如何将Eclipse中的项目迁移到Android Studio 中

    如何将Eclipse中的项目迁移到Android Studio 中 如果你之前有用Eclipse做过安卓开发,现在想要把Eclipse中的项目导入到Android Studio的环境中,那么首先要做的 ...

  5. Android开发的小技巧,在Android Studio中使用Designtime Layout Attributes

    在编写xml文件时,为了预览效果,经常会使用默认填上一些内容,比如TextView时,随便写上一个text <TextView ... android:text="Name:" ...

  6. 在android studio 中使用applicationid的问题

    现在我需要对项目app的某个功能做性能测试,主要测试耗电量的多少. 1.我想到的方式是,我需要在同一台手机测试,同一个应用,需要安装在手机两次,第二次安装不覆盖第一次的安装. 在android stu ...

  7. Android studio 中的配置编译错误总结

    1.编译Andorid 工程的时候,有时候出现gradle 报下面的错误. Error:(1, 0) Cause: com/android/build/gradle/LibraryPlugin : U ...

  8. Android Studio中清单文件改versionCode和versionName没效果的原因

    在Android Studio中,项目的versionCode 和versionName 的控制不是在AndroidManifest.xml清单文件中更改的,而是在项目的build.gradle中更改 ...

  9. android studio中如何设置注释模板

    在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android studio中该如何设置呢? 工具/原料   andro ...

随机推荐

  1. 关于PS的操作

    1.移动工具 Ctrl+J:拷贝图层 Ctrl+T:自由变换调整大小 Alt+Shift:全选 Ctrl+G:图层编组 Alt+Delete:选中图层填充当前颜色 Ctrl+Alt+Z:后退一步 2. ...

  2. leetcode105

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  3. ElasicSearch(2) Linux运行

    1.org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearc ...

  4. 尚硅谷springboot学习30-docker安装mysql示例

    docker pull mysql 错误的启动示例 错误日志:需要设置密码 正确的启动 但还不能直接使用,因为没有做端口映射,外界无法连接 可用的启动 连接成功 几个高级的操作 指定配置文件 dock ...

  5. jscript DOM操作

    \n 换行符 \b 空格 \r 回车 && 与 || 或 ! 非(取反) classList属性 classList 属性返回元素的类名,作为 DOMTokenList 对象. 该属性 ...

  6. canvas刮刮乐游戏等

    裁剪 ctx.clip():当前路径外的区域不再绘制 <canvas id="cans" width=500 height=500></canvas> &l ...

  7. 2、python的变量

    1.什么是变量 变量>顾名思义变化的量,量是一种表示一种状态的方式,而且可以变 2.为什么要用变量 程序的执行是一直处于一种变化状态的,我们可以用变量表示表示程序进行中的状态,并将它记录下来 3 ...

  8. lvs+nginx负载均衡

    1       学习目标 掌握什么是负载均衡及负载均衡的作用和意义. 了解lvs负载均衡的三种模式. 了解lvs-DR负载均衡部署方法. 掌握nginx实现负载均衡的方法. 掌握lvs+nginx负载 ...

  9. selenium之 chromedriver与chrome版本映射表(转载)

    chromedriver版本 支持的Chrome版本 v2.34 v61-63 v2.33 v60-62 v2.32 v59-61 v2.31 v58-60 v2.30 v58-60 v2.29 v5 ...

  10. day37 异步回调和协程

    异步回调 """ 异步任务使用场景 爬虫 1.从目标站点下载网页数据 本质就是HTML格式字符串 2.用re从字符串中提取出你需要的数据 ""&quo ...