【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;这时会出现容器里的文字也跟着透明.解决办法是不用十六进制的色值和透明度分开写,使 ...
随机推荐
- parallelogram
The parallelogram law in inner product spaces Vectors involved in the parallelogram law. In a normed ...
- 非常有助于理解二极管PN结原理的资料
https://www.zhihu.com/question/60053574/answer/174137061 我理解的半导体 pn 结的原理,哪里错了? https://blog.csdn.net ...
- 因子分析(Factor analysis)
1.引言 在高斯混合和EM算法中,我们运用EM算法拟合混合模型,但是我们得考虑得需要多少的样本数据才能准确识别出数据中的多个高斯模型!看下面两种情况的分析: 第一种情况假如有 m 个样本,每个样本的维 ...
- windows下nodejs服务器的安装与配置
1下载安装 download from the link: https://nodejs.org/en/ windows下的安装直接运行exe,略过-- 注:由于用户权限的问题,最好将nodejs安装 ...
- Android 中关于 【Cursor】 类的介绍
转自(http://www.cnblogs.com/TerryBlog/archive/2010/07/05/1771459.html) 使用过 SQLite 数据库的童鞋对 Cursor 应该不陌生 ...
- urb传输的代码分析【转】
转自:http://blog.csdn.net/zkami/article/details/2503829 urb传输的代码分析 如需引用,请注明出处blog.csdn.net/zkami 作者Zhe ...
- spring mvc file upload
文件上传 1.需要导入两个jar包 2.在SpringMVC配置文件中加入 1 2 3 4 <!-- upload settings --> <bean id="multi ...
- %08lx
u-boot中代码如下: debug ("Now running in RAM - U-Boot at: %08lx\n", dest_addr); 对应设备上的打印消息如下: N ...
- sublime sftp 打开远程文件夹
2014-04-29 13:19:09 总结: 本文介绍两种方法,推荐第二种方法(samba+windows映射) 先贴出sublime打开远程(Linux)目录所需的配置文件(sublime是通过s ...
- selnium远程机上传图片遇到的坑
一般上传图片方法采取方案如下: input标签的file类型上传图片,使用对象的sendkeys+路径方法 使用js注入,再用使用对象的sendkeys+路径方法 使用autolt生成的exe,打开对 ...