【问题标题】:android - tabs: custom bg color and text positionandroid - 选项卡:自定义背景颜色和文本位置
【发布时间】:2011-07-13 13:11:42
【问题描述】:

简单的问题(我猜)。我有这个用于标签的默认谷歌教程代码:

    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;
    } 

我想知道 - 是否可以仅通过向现有部分添加代码行来更改文本位置和(或)背景,而不是重新开始所有内容?谢谢!

是的:文本位置和自定义背景(html 颜色)。我该怎么做?谢谢!

P.S 请不要开始删除其他教程的链接,我很难理解这些。 :/

【问题讨论】:

    标签: android colors tabs position


    【解决方案1】:

    我找到了我正在寻找的代码。这是我的 Tabs.java 活动的完整代码:

    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"));
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-15
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2017-04-24
      • 1970-01-01
      相关资源
      最近更新 更多