2009-12-06 75 views
4

我有2個關於tabHost問題: 我創建tabHost有2個選項卡 和我用setIndicator(TextView的) (我用API級別4工作) 我的標題背景是白色標籤的標題。我使用標題的選擇器在標題的差異圖像之間進行選擇。安卓tabHost

  1. 我只想在選定/按下時將標題文字加粗。我沒有成功地使用我選擇的選擇器。我能做到嗎?我的想法是,在我使用drawable的情況下,我希望文本爲粗體。其他案件不粗體。同樣的問題關於textColor。

  2. 它看起來像一個錯誤 - 當選項卡第一次打開時,選定選項卡上的文本(我在tabHost.setCurrentTab(tabId)中使用的那個)根本看不到。在第一次按/​​聚焦/聚焦任何其他項目後,它看起來不錯。任何想法爲什麼或如何解決這個問題?

在此先感謝

上tabActivity -

TextView title1 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL); 
TextView title2 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL); 

title1.setText("teb11 title"); 
title1.setBackgroundResource(R.drawable.tabtitle); 
title1.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab1), null, null, null); 

title2.setText("tab22 title"); 
title2.setBackgroundResource(R.drawable.tabtitle); 

title2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab2), null, null, null); 

TabSpec tab1 = mTabHost.newTabSpec("tab1").setIndicator(title1).setContent(R.id.list1); 
TabSpec tab2 = mTabHost.newTabSpec("tab2").setIndicator(title2).setContent(R.id.list2); 

mTabHost.addTab(tab1); 
mTabHost.addTab(tab2);  
mTabHost.setCurrentTab(0); 

選擇tab1.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true"  
    android:drawable="@drawable/iconselect"/> 
    <item android:state_pressed="true" 
    android:drawable="@drawable/iconselect"/> 
    <item android:drawable="@drawable/icon"/>   
</selector> 

選擇了tabTitle

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" 
    android:drawable="@drawable/tabselected"/> 
    <item android:state_selected="true"  
    android:drawable="@drawable/tab" /> 
    <item android:state_focused="true" 
    android:drawable="@drawable/tab" /> 
</selector> 

回答

0

我可以看到您的佈局XML嗎?還是隻能通過Java命令來執行此操作?無論哪種方式,我希望看到的不僅僅是一些僞代碼來實際工作。

這將是您的main.xml文件或您的OnCreate()函數。如果您通過XML設置你的佈局,main.xml中應該有類似的內容:

<?xml version="1.0" encoding="utf-8"?> 
<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"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <RelativeLayout android:id="@+id/RelativeLayout01" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:background="#ffffffff"> 
      </RelativeLayout> 
      <RelativeLayout android:id="@+id/RelativeLayout02" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:background="#ffffffff"> 
      </RelativeLayout> 
     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

有可能通過Java命令來做到這一點完全,尤其是固定#2的時候,我們可能需要看到更多的代碼。作爲OnCreate()函數的一部分,我會建議初始化所有你想要顯示的東西。否則,如果它不在main.xml中,並且未初始化,則不會顯示。

+1

我沒有找到一種方法來初始化XML的標籤標題,我很樂意看到一個例如。 – galia 2009-12-07 08:22:16

0

這是我的XML

<?xml version="1.0" encoding="utf-8"?> 
<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"> 
    <LinearLayout android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="60px" 
      android:background="@drawable/headerbk" 
      android:paddingTop="12px" 
      android:orientation="vertical" 
      android:gravity="center_horizontal"> 
     </TabWidget> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent"    
      android:layout_height="fill_parent"> 
      <ListView android:id="@+id/list1" 
       android:background="@drawable/bk" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:scrollbars="vertical" 
       android:divider="@drawable/separationline" 
       android:cacheColorHint="#00000000" 
       android:footerDividersEnabled="false"     
       android:listSelector="@drawable/selected" />  
      <ListView android:id="@+id/list2" 
       android:background="@drawable/bk" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:scrollbars="vertical" 
       android:divider="@drawable/separationline" 
       android:cacheColorHint="#00000000" 
       android:footerDividersEnabled="false" 
       android:listSelector="@drawable/selected" />      
      </FrameLayout> 
    </LinearLayout> 
</TabHost>  
0

如果希望在選擇中的粗體文字,

有可能在不同的方式,但我所做的是...

進行自定義TextView的,和然後處理觸摸事件就像在操作時加粗一樣,並在操作時設置爲正常。

1

對於你的問題#1:
在您TabsAdapter使用onPageSelected(int position)這樣的:

public void onPageSelected(int position) { 
     //your logic here 
     fixTitleText(); 
} 

和:

private void fixTitleText() { 
     for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { 
      View view = mTabHost.getTabWidget().getChildAt(i); 
      TextView tv = (TextView) view.findViewById(android.R.id.title); 
      tv.setTextColor(getResources().getColor(R.drawable.text_selector)); 
      tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); 
      tv.setTypeface(null, Typeface.BOLD); 
     } 
} 

只是注意到,此代碼是用於蜂窩及以上,在此之前的標籤主機視圖層次有點不同