2012-02-10 9 views

risposta

11

credo, il seguente approccio è la più semplice . Hai solo bisogno di impostare le seguenti disegnabile (in realtà, è drawable di default di Android per le schede) come sfondo delle schede:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- Non focused states --> 
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_unselected" /> 
    <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_selected" /> 
    <!-- Focused states --> 
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
    <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/tab_focus" /> 
    <!-- Pressed --> 
    <item android:state_pressed="true" android:drawable="@drawable/tab_press" /> 
</selector> 

dove tab_press, tab_focus e tab_selected drawable sarebbero png di (preferirei 9-patches) con freccia verso il basso e regione trasparente vicino. tab_unselected disegnabile non avrebbe questa freccia, ma avrebbe ancora la stessa regione trasparente. L'unica cosa che resta da fare è specificare il margine inferiore negativo per il tuo TabWidget. Il suo valore è determinato dall'altezza della freccia (non dimenticare di usare density independent unità):

explanatory scheme

+0

Questo è aiutato un margine abot po bit.just abbagliati –

+0

Guarda l'immagine che ho aggiunto. Dovrebbe chiarire il mio suggerimento. –

+0

davvero una grande risposta. – Akram

-2

È possibile aggiungere immagini a disposizione con schede:

<RelativeLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="0dip" /> 
    <FrameLayout 
     android:fadingEdge="none" 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="0px" 
     android:layout_below="@android:id/tabs" 
     android:layout_alignParentBottom="true" 
     android:padding="0px" /> 
    <ImageView 
     .... 
     android:id="@+id/down_arrow_left"/> 
    <ImageView 
     .... 
     android:id="@+id/down_arrow_right"/> 
</RelativeLayout> 

e aggiungere ascoltatore nella vostra attività scheda:

getTabHost().setOnTabChangedListener(new OnTabChangeListener() { 
     public void onTabChanged(String tabId) { 
      if (tabId.equels("left")){ 
       findViewById(R.id.down_arrow_left).setVisibility(View.VISIBLE); 
       findViewById(R.id.down_arrow_right).setVisibility(View.INVISIBLE); 
      } else if (tabId.equels("right")){ 
       findViewById(R.id.down_arrow_left).setVisibility(View.INVISIBLE); 
       findViewById(R.id.down_arrow_right).setVisibility(View.VISIBLE); 
      } 
     } 
    }); 
0

tab_0_info.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/ic_menu_yourImg_selected" 
     android:state_selected="true" /> 
    <item android:drawable="@drawable/ic_menu_yourImg" /> 
</selector> 


private void addTab(int resouceTabId, int drawableId, 
     Class<? extends ActivityGroup> groupActivityClass) 
{ 
    Intent intent = new Intent(this, groupActivityClass); 
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + resouceTabId); 

    View tabIndicator = LayoutInflater.from(this).inflate(
      R.layout.tab_indicator, getTabWidget(), false); 

    TextView title = (TextView) tabIndicator.findViewById(R.id.title); 
    title.setText(resouceTabId); 
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 
    icon.setImageResource(drawableId); 

    spec.setIndicator(tabIndicator); 
    spec.setContent(intent); 
    tabHost.addTab(spec); 

} 

//addTab(R.string.yourTabTitle, R.drawable.tab_0_info, YourGroup.class); 
Problemi correlati