一般来说,我都是用Android默认的Tab,但此时Android会根据你增加的Tab页面平均分配Tab标签,假如你只有两个Tab,那么长度将会很长,并且其高度略微过高,并不好看,网上解决这个问题有些是自定义Tab。我们仍然使用默认的Tab解决此问题。


修改前 代码、图片如下:

import android.app.TabActivity;import android.graphics.Color;import android.os.Bundle;import android.view.LayoutInflater;import android.widget.TabHost;import android.widget.TabHost.OnTabChangeListener;public class MyTab extends TabActivity implements OnTabChangeListener {private TabHost myTabhost;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);myTabhost = this.getTabHost();// get TabhostLayoutInflater.from(this).inflate(R.layout.main,myTabhost.getTabContentView(), true);myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));myTabhost.addTab(myTabhost.newTabSpec("One")// make a new Tab.setIndicator("查询信息", getResources().getDrawable(R.drawable.gimp))// set the Title and Icon.setContent(R.id.widget_layout_Blue));// set the layoutmyTabhost.addTab(myTabhost.newTabSpec("Two")// make a new Tab.setIndicator("签到",getResources().getDrawable(R.drawable.mumule))// set the Title and Icon.setContent(R.id.widget_layout_green));}@Overridepublic void onTabChanged(String tagString) {// TODO Auto-generated method stub}}




下面我就如何 改变Tab标签大小及间距作出说明:

我在网上查阅文档,如果要改变此大小或者间距,必须将标签栏宽高设为 wrapContent(其他的我尚未验证):以下为修改代码及其效果:


import android.app.TabActivity;import android.graphics.Color;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.widget.LinearLayout;import android.widget.TabHost;import android.widget.TabWidget;import android.widget.TabHost.OnTabChangeListener;public class MyTab extends TabActivity implements OnTabChangeListener {private TabHost myTabhost;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);myTabhost = this.getTabHost();// get TabhostLayoutInflater.from(this).inflate(R.layout.main,myTabhost.getTabContentView(), true);myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150));myTabhost.addTab(myTabhost.newTabSpec("One")// make a new Tab.setIndicator("查询信息", getResources().getDrawable(R.drawable.gimp))// set the Title and Icon.setContent(R.id.widget_layout_Blue));// set the layoutmyTabhost.addTab(myTabhost.newTabSpec("Two")// make a new Tab.setIndicator("签到",getResources().getDrawable(R.drawable.mumule))// set the Title and Icon.setContent(R.id.widget_layout_green));// 首先得到标签栏TabWidget tabWidget = myTabhost.getTabWidget();//设置标签栏宽高为 WRAP_CONTENTLinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);tabWidget.setLayoutParams(params);int count = tabWidget.getChildCount();//得到每一个标签,并设置其宽高 for (int i = 0; i < count; i++) {   View view = tabWidget.getChildTabViewAt(i);      view.getLayoutParams().height = 50; //tabWidget.getChildAt(i)   view.getLayoutParams().width = 80;  }}@Overridepublic void onTabChanged(String tagString) {// TODO Auto-generated method stub}}





更多相关文章

  1. Android之UI学习篇十:使用TabHost实现微博客户端界面
  2. Android利用SAX解析XML文件
  3. Android(安卓)背景透明度设置和设置GridView元素间距
  4. android pull dom解析xml
  5. Android(安卓)三角标签(自定义Textview控件)
  6. 【Android外文翻译 - 03】创建可复用的UI组件
  7. android 重写TextView 实现文字整齐排版
  8. Android(安卓)setTag和getTag()的使用
  9. Tabwidget- TabWidget选择标签

随机推荐

  1. Android水波纹效果顺手拈来!
  2. Android(安卓)wifi信号强弱检测
  3. android对话框弹出方式动画
  4. Android(安卓)API level与version对应关
  5. 多个Android(安卓)device offline处理命
  6. NDK的扩展
  7. android 保存和读取文件
  8. android---菜单栏选项
  9. android listView控件用法
  10. android 6.0锁屏界面时间位置修改