【Android基础篇】TabWidget设置背景和字体
在使用TabHost实现底部导航栏时,底部导航栏的三个导航button无法在布局文件中进行定制。比方设置点击时的颜色、字体的大小及颜色等,这里提供了一个解决的方法。就是在代码里进行定制。
思路是在Activity里给TabHost加入了分页后,在给导航栏TabWidget的导航button逐个加入特效(必须先加入分页。然后才干定制button,加入了一个分页,才会生成一个button)。
以下是布局文件activity_main.xml,包括了TabHost,里面有三个仅仅显示了文字的分页
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.plan.MainActivity"
tools:ignore="MergeRootFrame" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.8" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3" />
</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
</LinearLayout>
</TabHost>
</RelativeLayout>
以下是MainActivity里的代码:
package com.aiplan_03;
import android.app.ActivityGroup;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
public class MainActivity extends ActivityGroup {
TabHost mTabHost = null;
TabWidget mTabWidget = null; //TabWidget控件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this.getLocalActivityManager());
//获取导航button控件
mTabWidget = mTabHost.getTabWidget();
//加入分页1
mTabHost.addTab(mTabHost.newTabSpec("button1").setContent(
R.id.tab1).setIndicator("btn1"));
//加入分页2
mTabHost.addTab(mTabHost.newTabSpec("button2").setContent(
R.id.tab2).setIndicator("btn2"));
//加入分页3
mTabHost.addTab(mTabHost.newTabSpec("button3").setContent(
R.id.tab3).setIndicator("btn3"));
Log.d("button数",Integer.toString(mTabWidget.getChildCount()));
//逐个button加入特效
for(int i=0;i<mTabWidget.getChildCount();i++){
//换字体颜色
TextView tv = (TextView)
mTabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.rgb(255, 255, 255));
//设置背景图
mTabWidget.getChildAt(i).setBackgroundResource(
R.drawable.tabwidget_selector);
}
}
}
把导航button的字体换成了白色,给导航button的背景加入了一个selector选择器。以下是选择器代码:
tabwidget_selector.xml。需放到drawable目录下
<?
xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_selected="false"
android:drawable="@color/tabwidget_unselected"
/>
<item
android:state_selected="true"
android:drawable="@color/tabwidget_selected" />
</selector>
里面用到了两个颜色。以下是color.xml,需放到values目录下
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<color name="tabwidget_selected">#ff2222</color>
<color name="tabwidget_unselected">#000000</color>
</resources>
最后的效果图例如以下:
【Android基础篇】TabWidget设置背景和字体的更多相关文章
- 深入理解gradle编译-Android基础篇
深入理解gradle编译-Android基础篇 导读 Gradle基于Groovy的特定领域语言(DSL)编写的一种自动化建构工具,Groovy作为一种高级语言由Java代码实现,本文将对Gradle ...
- android基础篇学习心得
android技术中,线程.进程.JNI.IPC和各个小框架结构是基本功.在跟随高焕堂老师的android程序猿到架构师之路系列视频中 学习完基础篇之后,颇有些心得,记录下来. android开发就是 ...
- Android基础TOP5_5:设置没有标题栏而且用系统壁纸当背景的界面
在res/values目录下的style.xml设置如下 <style name="AppBaseTheme" parent="android:Theme.Wall ...
- Android 基础篇(二)
ADB进程 adb指令 adb install xxx.apk adb uninstall 包名 adb devices adb start-server adb kill-server adb sh ...
- 安卓工作室android studio 美化 ,设置背景图片。
作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com sexy Editor 点击file-> ...
- Android基础TOP2:单机按钮改变字体颜色
---恢复内容开始--- Activity: <TextView android:id="@+id/t1" android:textSize="30dp" ...
- Android基础篇(一)
Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...
- android基础篇------------java基础(12)(多线程操作)
<一>基本概念理解 1.什么是进程? 进程就是在某种程度上相互隔离,独立运行的程序.一般来说,系统都是支持多进程操作的,这所谓的多进程就是让系统好像同时运行多个程序. 2.什么是线程呢? ...
- CSS设置背景透明字体不透明
写CSS时给容器设置透明度的时候如果使用background-color: #000000; opacity: 0.5;这时会出现容器里的文字也跟着透明.解决办法是不用十六进制的色值和透明度分开写,使 ...
随机推荐
- Python人工智能之路 - 第一篇 : 你得会点儿Python基础
Python 号称是最接近人工智能的语言,因为它的动态便捷性和灵活的三方扩展,成就了它在人工智能领域的丰碑 走进Python,靠近人工智能 一.编程语言Python的基础 之 "浅入浅出&q ...
- State Estimation for Robotics (Tim Barfoot) exercises Answers
Here are some exercises answers for State Estimation for Robotics, which I did in June, 2017. The bo ...
- crontab每10秒钟执行一次
1.使用sleep 在crontab中加入 * * * * * sleep 10; /bin/date >>/tmp/date.txt* * * * * sleep 20; /bin/da ...
- python的map/reduce区别
直接上列子 map: 把f(x)作用在list的每一个元素并把结果生成一个新的list” # coding=utf-8def f(x): return x * x r = map(f, [1, 2, ...
- 001_fpm打包命令详解
使用fpm来制作rpm包 2017/2/22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...
- 在Scrapy项目【内外】使用scrapy shell命令抓取 某网站首页的初步情况
Windows 10家庭中文版,Python 3.6.3,Scrapy 1.5.0, 时隔一月,再次玩Scrapy项目,希望这次可以玩的更进一步. 本文展示使用在 Scrapy项目内.项目外scrap ...
- Ibatis.Net 数据库操作学习(四)
一.查询select 还记得第一篇示例中是如何读出数据库里3条数据的吗?就是调用了一个QueryForList方法,从方法名就知道,查询返回列表. 1.QueryForList 返回List< ...
- 移动端console.log()调试
在微信或app进行开发的时候,没法直接查看console.log的输出内容,调试起来简直太痛苦了. 1.笨笨的方法 fiddler抓请求:追加dom节点,显示调试信息. var div =docume ...
- 2018-11-1 NOIP 模拟赛解题报告
T1 Domino 多米诺骨牌 题目大意 给你N个骨牌,上下各有一个数,要使上面一排的和为偶数,同时下面一排的和也为偶数,最多要翻转多少次?如果无法达成那么输出-1. 解法 水题秒切 根据数的奇偶性质 ...
- webstorm减少内存占用
首先,按照我说的设置之后要重启才行. 在项目里找到不需要监听的文件夹右键:Mark Directory As => Cancel Exclusion 然后重启,嘿嘿,成功了!