2013-01-03 37 views
3

我們有一個關於Android中的TabHost-widget的奇怪問題。我們如何解決這個像素行錯誤(下面的圖片上顯示):如何在使用TabHost時刪除像素錯誤

Pixel error

感謝您的幫助。

+3

請分享您的代碼,創建標籤。你是否使用自己的分隔符drawable? –

+0

你見過我的回答嗎? –

+0

你有試過我的代碼嗎?結果是什麼 ??? –

回答

0

我有三個選項卡和一個自定義分隔符,但是所有五個選項卡都使用自定義xml(customtabview.xml)。

Tabs.java

tabHost = getTabHost(); 
    TabHost.TabSpec spec; 

    LinearLayout v = (LinearLayout)(getLayoutInflater().inflate(R.layout.customtabview,  null)); 

    //FIRST TAB 
    ((TextView)v.findViewById(R.id.title)).setText("FIRST"); 
    ((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable .tab_back,0,0); 
    spec = tabHost.newTabSpec("first").setIndicator(v).setContent(R.id.tab_first_info); 
    tabHost.addTab(spec); 

    //SECOND TAB 
    ((TextView)v.findViewById(R.id.title)).setText("SECOND"); 
    ((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0); 
    spec = tabHost.newTabSpec("second").setIndicator(v).setContent(R.id.tab_second_info); 
    tabHost.addTab(spec); 

    //THIRD TAB 
    ((TextView)v.findViewById(R.id.title)).setText("THIRD"); 
    ((TextView)v.findViewById(R.id.title)).setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.tab_back,0,0); 
    spec = tabHost.newTabSpec("third").setIndicator(v).setContent(R.id.tab_third_info); 
    tabHost.addTab(spec); 

customtabview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center" > 

     <TextView 
      android:id="@+id/title" 
      android:drawablePadding="4dp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      /> 

</LinearLayout> 
0

我也是前幾天有同樣的問題。但是現在通過將其更改爲圖像獲得解決方案。 默認情況下第一個選項卡被選中,當選項卡變更時,所有選項卡會根據選項卡選項自動更改。

請參見下面的代碼:

public class MainTabActivity extends TabActivity { 
    TabHost mTabHost; 
    TabWidget mTabWidget; 
    private TextView title; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_tab_layout); 

     //Resources res = getResources(); 
     mTabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     mTabWidget = mTabHost.getTabWidget(); 
     Intent intent; 

     intent = new Intent().setClass(MainTabActivity.this, TodaysDealsGroupActivity.class); 
     spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_todays_deal)).setIndicator(getResources().getString(R.string.tab_todays_deal), getResources().getDrawable(R.drawable.tab_loyalshop_selector)).setContent(intent); 
     mTabHost.addTab(spec); 

     intent = new Intent().setClass(MainTabActivity.this, BuddiesGroupActivity.class); 
     spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_buddies)).setIndicator(getResources().getString(R.string.tab_buddies), getResources().getDrawable(R.drawable.tab_buddies_selector)).setContent(intent); 
     mTabHost.addTab(spec); 

     intent = new Intent().setClass(MainTabActivity.this, SearchGroupActivity.class); 
     spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_search)).setIndicator(getResources().getString(R.string.tab_search), getResources().getDrawable(R.drawable.tab_search_selector)).setContent(intent); 
     mTabHost.addTab(spec); 

     intent = new Intent().setClass(MainTabActivity.this, ProfileGroupActivity.class); 
     spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_profile)).setIndicator(getResources().getString(R.string.tab_profile), getResources().getDrawable(R.drawable.tab_profile_selector)).setContent(intent); 
     mTabHost.addTab(spec); 

     intent = new Intent().setClass(MainTabActivity.this, NotificationsGroupActivity.class); 
     spec = mTabHost.newTabSpec(getResources().getString(R.string.tab_notifications)).setIndicator(getResources().getString(R.string.tab_notifications), getResources().getDrawable(R.drawable.tab_notification_selector)).setContent(intent); 
     mTabHost.addTab(spec); 

     for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { 
      mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg)); 
      title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title); 
      title.setTextColor(Color.WHITE); 
      title.setTextSize(10); 
     } 

     // check if App starts from the Notification click or not 
     if(getIntent().hasExtra("notification")){ 
      // for the current tab selection 
      mTabHost.getTabWidget().getChildAt(4).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg)); 
      mTabHost.setCurrentTab(4); 
      title = (TextView) mTabWidget.getChildAt(4).findViewById(android.R.id.title); 

     }else{ 
      // for the current tab selection 
      mTabHost.getTabWidget().getChildAt(0).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg)); 
      mTabHost.setCurrentTab(0); 
      title = (TextView) mTabWidget.getChildAt(0).findViewById(android.R.id.title); 

     } 


     title.setTextColor(Color.BLACK); 
     title.setTextSize(10); 

     // listener for the tab changed 
     mTabHost.setOnTabChangedListener(new OnTabChangeListener() { 
      @Override 
      public void onTabChanged(String tabId) { 

       for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { 
        mTabHost.getTabWidget().getChildAt(i).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_black_bg)); 
        title = (TextView) mTabWidget.getChildAt(i).findViewById(android.R.id.title); 
        title.setTextColor(Color.WHITE); 
        title.setTextSize(10); 
       } 

       int tab = mTabHost.getCurrentTab(); 

       mTabHost.getTabWidget().getChildAt(tab).setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_blue_bg)); 
       title = (TextView) mTabWidget.getChildAt(tab).findViewById(android.R.id.title); 
       title.setTextColor(Color.BLACK); 
       title.setTextSize(10); 
      } 
     }); 
    } 
} 

這裏是XML文件main_tab_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TabHost 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/tabhost" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center" 
      android:orientation="vertical" > 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" /> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:gravity="bottom|center_horizontal" 
       android:tabStripEnabled="false" > 
      </TabWidget> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

與您的圖像只需更換和看到的結果。

希望這會幫助你很多,因爲它可以幫助我。

享受編碼...