Android 默认Tab标签大小及间距修改
一般来说,我都是用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;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- myTabhost = this.getTabHost();
- // get Tabhost
- LayoutInflater.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 layout
- myTabhost.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));
- }
- @Override
- public 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;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- myTabhost = this.getTabHost();
- // get Tabhost
- LayoutInflater.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 layout
- myTabhost.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_CONTENT
- LinearLayout.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;
- }
- }
- @Override
- public void onTabChanged(String tagString) {
- // TODO Auto-generated method stub
- }
- }