2012-05-24 12 views
17

Zaczynam od Androida i kiedy próbuję zrobić TabHost z ikoną i tekstem. Tylko tekst jest widoczny, jeśli pozostawię tekst pusty, można zobaczyć ikonę. Chcę zobaczyć oba na ekranie.Ikona na karcie nie jest wyświetlana

Czy ktoś może udzielić mi porady?

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

Usuario usu = new Usuario(); 
usu.nombre = "fsdf"; 
lista.add(usu); 

adaptador = new AdaptadorCelda(this,lista); 
ListView lstView = (ListView)findViewById(R.id.lista); 
lstView.setAdapter(adaptador); 

Button btn = (Button)findViewById(R.id.btnSalva); 

btn.setOnClickListener(onSave); 



Resources res = getResources(); 

TabHost tabs=(TabHost)findViewById(R.id.tabhost); 
tabs.setup(); 

TabHost.TabSpec spec=tabs.newTabSpec("mitab1"); 
spec.setContent(R.id.tab1); 
spec.setIndicator("",res.getDrawable(android.R.drawable.ic_menu_rotate)); 


tabs.addTab(spec); 

spec=tabs.newTabSpec("mitab2"); 
spec.setContent(R.id.tab2); 
spec.setIndicator("ddd", 
     res.getDrawable(android.R.drawable.presence_invisible)); 
tabs.addTab(spec); 

tabs.setCurrentTab(0); 

spec.setIndicator("",res.getDrawable(android.R.drawable.ic_menu_rotate)) In this case icon appears. 

spec.setIndicator("ddd", 
     res.getDrawable(android.R.drawable.presence_invisible)); 

i tutaj jest main.xml

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

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

<TabWidget 
    android:id="@android:id/tabs" 
    android:layout_width="match_parent" 

    android:layout_height="wrap_content" /> 

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/tab1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 
     <TextView 
      android:id="@+id/lblNombre" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_marginTop="17dp" 
      android:text="Nombre" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <EditText 
      android:id="@+id/txtNombre" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignTop="@+id/lblNombre" 
      android:layout_marginLeft="35dp" 
      android:layout_toRightOf="@+id/lblNombre" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/txtApellido" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/txtNombre" 
      android:layout_below="@+id/txtNombre" 
      android:ems="10" /> 

     <TextView 
      android:id="@+id/lblApellido" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/txtApellido" 
      android:layout_alignParentLeft="true" 
      android:text="Apellidos" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <RadioGroup 
      android:id="@+id/tipos" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/txtApellido" 
      android:layout_marginTop="35dp" 
      android:layout_toLeftOf="@+id/txtApellido" > 

      <RadioButton 
       android:id="@+id/rdbAlta" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="alta" /> 

      <RadioButton 
       android:id="@+id/rdbBaja" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="baja" /> 

      <RadioButton 
       android:id="@+id/rdbTramite" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="tramite" /> 
     </RadioGroup> 

     <Button 
      android:id="@+id/btnSalva" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/txtApellido" 
      android:layout_alignTop="@+id/tipos" 
      android:layout_marginLeft="58dp" 
      android:layout_marginTop="30dp" 
      android:text="Button" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_below="@+id/txtApellido" 
      android:src="@drawable/ic_menu_chart" /> 

    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/tab2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <ListView 
      android:id="@+id/lista" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/tipos" 
      android:layout_marginTop="24dp" > 
     </ListView> 
    </LinearLayout> 
</FrameLayout> 

+2

Pokaż nam co robisz –

+0

proszę dać używać więcej informacji .. –

+0

Sorry chłopaki, jestem nieco początkującym. – theholy

Odpowiedz

52

Czy przetestowaniu aplikację na urządzeniu z systemem Ice Cream Sandwich? Ostatnio zauważyłem, że domyślna TabHost zachowuje się inaczej w zależności od urządzenia docelowego i wersji platformy Android.

uruchomiona aplikacja, która udostępnia ikony i tekst setIndicator (jak w źródle z pytaniem), miał następujący wynik testu:

  • Telefon z Androidem 2.1: zarówno ikony i tekst, w którym pokazane (Etykieta pod ikoną zgodnie z oczekiwaniami)
  • Telefon z opcją 4.0.3: Ikony nie były wyświetlane, a widoczny był tylko tekst.
  • Tablet z systemem ICS: klapki, gdzie pokazane z ikoną i tekstu

Jak dowiedział się też, zastępując etykietę z pusty ciąg z ikony pojawiły na telefon - ale wówczas użytkownik będzie trzeba odgadnąć znaczenie ikon. Poza tym: Podczas uruchamiania tej wersji aplikacji na telefonie poprzedzającym ICS: karty zachowują swój rozmiar i pozostawiają pusty obszar pod obrazem.

Aby zmienić tę decyzję na urządzenie napędzane ile miejsca zaczepy powinny dostać, znalazłem rozwiązanie w tym tutorial on how to customize tabs:

najpierw trzeba podać swoje własną wskaźnikową Wypustka układ ("układ/tab_indicator.xml "w samouczku), w tym ImageView i TextView. Następnie powiedz TabSpec, aby użyć tego poprzez setIndicator. I voila: dostaniesz ikonę i etykietę na telefonie z ICS.

Oto ważnym elementem, w jaki sposób skonfigurować wskaźnik (ta = bieżąca aktywność):

TabHost.TabSpec spec = this.getTabHost().newTabSpec(tag); 

View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false); 
((TextView) tabIndicator.findViewById(R.id.title)).setText(label); 
((ImageView) tabIndicator.findViewById(R.id.icon)).setImageResource(drawableResourceId); 

spec.setIndicator(tabIndicator); 

Jeśli lubisz ten sam wygląd zakładek na starszych i nowszych telefonach mają dodatkowy wygląd tutaj : http://jgilfelt.github.com/android-actionbarstylegenerator/. Ta strona generuje obrazy i style, aby dostosować pasek akcji, w tym karty, i pomógł mi dowiedzieć się, jak należy zdefiniować 9-plasterkowe obrazy tła.

+2

Świetnie, powinieneś zrobić samouczki. Prawdziwe łatwe do odczytania i zrozumienia. –

+0

Również wydaje się, że używam bardziej aktualnych fragmentów. Każdy pomysł, jak zaimplementować to na fragmenty zamiast za pomocą starej TabActivity? –

+2

To podejście usuwa także "cienką linię" pod obrazem/tekstem po wybraniu karty. –

2

próbuję następujący sposób to zrobić i to działa dobrze:

  1. użyciu setIndicator ("Karta"), aby ustawić tylko tekst.
  2. użycie setBackgroundDrawable (-) do ustawienia ikony.

Przykładowy kod:

final TabHost    tabHost = (TabHost)findViewById(android.R.id.tabhost); 
    final Resources    res = getResources(); 
    int i; 

    tabHost.setup(); //Call setup() before adding tabs if loading TabHost using findViewById(). 

    TabHost.TabSpec ts = tabHost.newTabSpec("trackinfo"); 
    //ts.setIndicator("", res.getDrawable(R.drawable.icon_trackinfo)); 
    ts.setIndicator("Track Information"); 
    ts.setContent(R.id.tabTrackInfo); 
    tabHost.addTab(ts); 

    TabHost.TabSpec ts2 = tabHost.newTabSpec("collection"); 
    //ts2.setIndicator("", res.getDrawable(R.drawable.icon_collection_gray)); 
    ts2.setIndicator("My Collection"); 
    ts2.setContent(R.id.tabMyCollecton); 
    tabHost.addTab(ts2); 

    tabHost.setOnTabChangedListener(new OnTabChangeListener(){ 
     @Override 
     public void onTabChanged(String tabId) 
     { 
      if("trackinfo".equals(tabId)) { 
       // 
       TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs); 
       View tabView = tw.getChildTabViewAt(0); 
       TextView tv = (TextView)tabView.findViewById(android.R.id.title); 
       tv.setTextColor(TabColor[0]); 
       tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_selected)); 

       tabView = tw.getChildTabViewAt(1); 
       tv = (TextView)tabView.findViewById(android.R.id.title); 
       tv.setTextColor(TabColor[1]); 

       tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_gray)); 
      } 
      if("collection".equals(tabId)) { 
       // 
       TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs); 
       View tabView = tw.getChildTabViewAt(0); 
       TextView tv = (TextView)tabView.findViewById(android.R.id.title); 
       tv.setTextColor(TabColor[1]); 
       tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_gray)); 

       tabView = tw.getChildTabViewAt(1); 
       tv = (TextView)tabView.findViewById(android.R.id.title); 
       tv.setTextColor(TabColor[0]); 
       tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_selected)); 

     } 
     }}); // END OF tabHost.setOnTabChangedListener 

    // After Tabs being added 
    // change font settings 
    TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs); 
    for(i=0;i<2;i++) 
    { 
     View tabView = tw.getChildTabViewAt(i); 
     TextView tv = (TextView)tabView.findViewById(android.R.id.title); 
     tv.setHeight(25); 
     tv.setTextColor(TabColor[i]); 
     tv.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC); 
     tv.setTextSize(20);  
     tabView.setBackgroundDrawable(res.getDrawable(TabIcon[i])); 

    } 
6

Jest jeszcze inny bardzo proste rozwiązanie tego problemu.

Wygląda na to, że problem jest w nowym motywie Holo i TabWidget. Więc co należy zrobić, to określić motyw Holo w folderze values-v11 ale ze starym stylu TabWidget tak:

<style name="MyAppTheme" parent="@android:style/Theme.Holo.NoActionBar"> 
    <item name="android:tabWidgetStyle">@android:style/Widget.TabWidget</item> 
</style> 

To działa dla mnie, mam nadzieję, że ktoś pomoże.

+0

Trzeba również dodać ten wiersz do ** values-v14 ** i folderu wyższej wersji. – biswajitGhosh

7

To jest łatwy sposób, aby pokazać tekst i ikonę w ICS. Po skonfigurować w Tabhost wzywam kolejnych metod:

setTabIcon(mTabHost, 0, R.drawable.ic_tab1); //for Tab 1 
setTabIcon(mTabHost, 1, R.drawable.ic_tab2); //for Tab 2 

public void setTabIcon(TabHost tabHost, int tabIndex, int iconResource) { 
    ImageView tabImageView = (ImageView) tabHost.getTabWidget().getChildTabViewAt(tabIndex).findViewById(android.R.id.icon); 
    tabImageView.setVisibility(View.VISIBLE); 
    tabImageView.setImageResource(iconResource); 
} 

Jeśli chcesz bardziej zaawansowane rozwiązania, trzeba utworzyć układ jak @sunadorer polecam. Moja rekomendacja jest taka, że ​​możesz stworzyć swój układ za pomocą układu motywów holo, przeszukać plik na swoim sdk_dir/platforms/android-14/data/res/layout/tab_indicator_holo.xml.

Moja zaawansowanym rozwiązaniem jest obok, trzeba utworzyć układ takiego:

tab_indicator.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingTop="5dp" 
       android:paddingBottom="5dp" 
       style="@android:style/Widget.Holo.Tab"> 

    <ImageView 
     android:id="@android:id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:visibility="visible" /> 

    <TextView 
     android:id="@android:id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     style="@android:style/Widget.Holo.ActionBar.TabText" /> 

</LinearLayout> 

W swojej działalności lub fragmentu, tworzyć następną funkcję:

public View createTabIndicator(LayoutInflater inflater, TabHost tabHost, int textResource, int iconResource) { 
    View tabIndicator = inflater.inflate(R.layout.tab_indicator, tabHost.getTabWidget(), false); 
    ((TextView) tabIndicator.findViewById(android.R.id.title)).setText(textResource); 
    ((ImageView) tabIndicator.findViewById(android.R.id.icon)).setImageResource(iconResource); 
    return tabIndicator; 
} 

I na koniec, po utworzeniu zakładki w swojej aktywności lub fragmencie, użyj następnego kodu:

mTabHost.addTab(
    mTabHost.newTabSpec("tab1") 
    .setIndicator(createTabIndicator(inflater, mTabHost, R.string.tab1, R.drawable.ic_tab1)) 
    .setContent(R.id.tab1) 
); 

Przetestowałem to rozwiązanie w ICS i działa dobrze.

Powodzenia :)

+1

Otrzymuję błąd w tej linii NPE: tabulator ImageViewImageView = (ImageView) tabHost.getTabWidget(). GetChildTabViewAt (tabIndex) .findViewById (android.R.id.icon); – user3233280

Powiązane problemy