How can we specify width for each tab in tabhost android

↘锁芯ラ 提交于 2019-12-25 05:22:24

问题


I am using tabhost in my application. Can we give separate width property for each tab? i.e, one with larger width and other with smaller?please give me sample example.if possible means give me code.

EDIT:

public class TabBarSimple extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);



    TabHost tabHost = getTabHost();
    TabHost.TabSpec spec;
    Intent intent;

    TabSpec dbspec = tabHost.newTabSpec("DashBoard");
    dbspec.setIndicator("Dashboard", getResources().getDrawable(R.drawable.dashboard));

    Intent dbIntent = new Intent(this, PhotosActivity.class);
    dbspec.setContent(dbIntent);
    tabHost.addTab(dbspec);

    TabSpec orderspec = tabHost.newTabSpec("Orders");
    orderspec.setIndicator("Orders", getResources().getDrawable(R.drawable.orders));

    Intent orderIntent = new Intent(this, SongsActivity.class);
    orderspec.setContent(orderIntent);
    tabHost.addTab(orderspec);
    TabSpec settingspec = tabHost.newTabSpec("Customers");
    settingspec.setIndicator("Customers", getResources().getDrawable(R.drawable.customers));
    Intent settingIntent = new Intent(this, VideosActivity.class);
    settingspec.setContent(settingIntent);
    tabHost.addTab(settingspec);
    TabSpec aboutspec = tabHost.newTabSpec("Settings");
    aboutspec.setIndicator("Settings", getResources().getDrawable(R.drawable.settings));
    Intent aboutIntent = new Intent(this, PhotosActivity.class);
    aboutspec.setContent(aboutIntent);
    tabHost.addTab(aboutspec);
    TabSpec logoutspec = tabHost.newTabSpec("Logout");
    logoutspec.setIndicator("Logout", getResources().getDrawable(R.drawable.logout));
    Intent logoutIntent = new Intent(this, SongsActivity.class);
    logoutspec.setContent(logoutIntent);
    tabHost.addTab(logoutspec);


    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
    //tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_indicator);
    tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 100;
    tabHost.getTabWidget().getChildAt(1).getLayoutParams().width = 50;
    }
    tabHost.getTabWidget().setCurrentTab(0);
    //tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#C35817"));
    tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_indicator);

     }

    public void onTabChanged(String tabId) {
          TabHost tabHost = getTabHost();
    // TODO Auto-generated method stub
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
    //tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#8A4117"));
    tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_indicator);
    }
    //tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#C35817"));
    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundResource(R.drawable.tab_indicator);
    }

     }

回答1:


You can achieve this by the code below -:

tabHost.getTabWidget().getChildAt(0).getLayoutParams().width = 50;


来源:https://stackoverflow.com/questions/12798732/how-can-we-specify-width-for-each-tab-in-tabhost-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!