android TabHost解決下面白線

來源:互聯網
上載者:User

  我之前做分頁都是用GridView和ActivityGroup實現的.因為覺得TabHost不好用,還有就是自己水平差的原因吧.如果幫的話,重寫view任何問題都可以解決,呵呵,下面請看實現過程,其實很簡單.
      針對TabHost的運用,我就不多講解了,網上例子好多,或者你也可以下載我的Demo查看,不過先聲明,寫的不好.
      程式碼片段:
[java] 
public class MyActivity extends TabActivity { 
    private TabWidget tabWidget; 
 
    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        Resources res = getResources(); // Resource object to get Drawables 
        final TabHost tabHost = getTabHost(); // The activity TabHost 
        TabHost.TabSpec spec; // Resusable 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, ArtistsActivity.class); 
 
        // Initialize a TabSpec for each tab and add it to the TabHost 
        spec = tabHost 
                .newTabSpec("artists") 
                .setIndicator("Artists", 
                        res.getDrawable(R.drawable.ic_tab_artists)) 
                .setContent(intent); 
        tabHost.addTab(spec); 
 
        // Do the same for the other tabs 
        intent = new Intent().setClass(this, AlbumsActivity.class); 
        spec = tabHost 
                .newTabSpec("albums") 
                .setIndicator("Albums", 
                        res.getDrawable(R.drawable.ic_tab_artists)) 
                .setContent(intent); 
        tabHost.addTab(spec); 
 
        intent = new Intent().setClass(this, SongsActivity.class); 
        spec = tabHost 
                .newTabSpec("songs") 
                .setIndicator("Songs", 
                        res.getDrawable(R.drawable.ic_tab_artists)) 
                .setContent(intent); 
        tabHost.addTab(spec); 
        tabHost.setCurrentTab(2); 
        View v; 
        tabWidget = tabHost.getTabWidget(); 
 
        for (int i = 0; i < tabWidget.getChildCount(); i++) { 
            // 擷取tabview項 
            v = tabWidget.getChildAt(i); 
            // 設定tab背景顏色 
            v.setBackgroundResource(android.R.color.white); 
            // 擷取textview控制項,(預設為白色) 
            TextView textView = (TextView) v.findViewById(android.R.id.title); 
            textView.setTextColor(Color.BLACK); 
            // 預設選項要處理 
            if (tabHost.getCurrentTab() == i) 
                v.setBackgroundResource(R.drawable.renren_sdk_pay_repair_btn); 
        } 
        // tabchanged的監聽 
        tabHost.setOnTabChangedListener(new OnTabChangeListener() { 
            // tabId顯示的是:newTabSpec裡面的值 
            @Override 
            public void onTabChanged(String tabId) { 
                // 首先把所有的view背景初始化了. 
                for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 
                    View v = tabHost.getTabWidget().getChildAt(i); 
                    // 設定tab背景顏色 
                    v.setBackgroundResource(android.R.color.white); 
                    // 選中的進行處理 
                    if (tabHost.getCurrentTab() == i) { 
                        v.setBackgroundResource(R.drawable.renren_sdk_pay_repair_btn); 
                    } 
 
                } 
            } 
        }); 
 
    } 

作者:jj120522

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.