开发者

android - tabs: custom bg color and text position

Simple question (I guess). I have this default google tutorial code for tabs:

    intent = new Intent().setClass(this, About.class);
    spec = tabHost.newTabSpec("albums").setIndicator("About")
                  .setContent(intent);
    tabHost.addTab(spec);
    for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
        tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;开发者_运维技巧
    } 

And I was wondering - is it possible to change text position and (or) background just by adding lines of code to the existing piece, not starting everything over again? Thanks!

So yeah: TEXT POSITION and CUSTOM BACKGROUND (html color). How do I do it? Thanks!

P.S Please don't start droping links to other tutorials, it's kinda hard for me to understand those. :/


I found the code I was looking for. Here's the full code of my Tabs.java activity:

package com.xjcdi.name;

import com.xjcdi.exploringvilnius.R;

public class Tabs extends TabActivity implements OnTabChangeListener {

    TabHost tabHost;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);

        tabHost = (TabHost)findViewById(android.R.id.tabhost);
        tabHost.setOnTabChangedListener(this);

        Resources res = getResources(); // Resource object to get drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Reusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Places.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Places")
                      .setContent(intent);
        tabHost.addTab(spec);
        for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
            tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
        }
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)



        // Do the same for the other tabs
        intent = new Intent().setClass(this, About.class);
        spec = tabHost.newTabSpec("albums").setIndicator("About")
                      .setContent(intent);
        tabHost.addTab(spec);
        for (int i = 1; i < tabHost.getTabWidget().getTabCount(); i++) {
            tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
        }

        intent = new Intent().setClass(this, Artistai.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Map")
                      .setContent(intent);
        tabHost.addTab(spec);
        for (int i = 2; i < tabHost.getTabWidget().getTabCount(); i++) {
            tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
        }

        intent = new Intent().setClass(this, Map.class);
        spec = tabHost.newTabSpec("songs").setIndicator("History")
                      .setContent(intent);
        tabHost.addTab(spec);
        for (int i = 3; i < tabHost.getTabWidget().getTabCount(); i++) {
            tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
        }



        tabHost.setCurrentTab(0);
    }

    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub
        for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
        {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#000000"));
        } 

        tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#c1902d"));
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜