2012-05-24 11 views
17

दिखा रहा है मैं एंड्रॉइड से शुरू कर रहा हूं और जब मैं आइकन और टेक्स्ट के साथ टैबहोस्ट करने का प्रयास करता हूं। केवल पाठ दिखाई देता है, अगर मैंने टेक्स्ट को खाली में छोड़ा तो आइकन देखना संभव है। मैं स्क्रीन पर दोनों देखना चाहता हूँ।टैब में आइकन

क्या कोई मुझे सलाह दे सकता है?

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)); 

और यहाँ 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

आप कर रहे हैं क्या –

+0

उपयोग में अधिक जानकारी दे कृपया .. –

+0

खेद लोग हमें दिखाएं, मैं थोड़ा नौसिखिया हूँ। – theholy

उत्तर

52

आप आइस क्रीम सैंडविच पर चल रहे डिवाइस पर अपने अनुप्रयोग का परीक्षण किया है है? मैंने हाल ही में देखा है कि डिफ़ॉल्ट डिवाइस TabHost लक्ष्य डिवाइस और एंड्रॉइड प्लेटफार्म संस्करण के आधार पर अलग-अलग व्यवहार करता है। एंड्रॉयड 2.1 के साथ

  • फोन:: दोनों आइकन और पाठ जहां से पता चला (

    करने वाले एप्लिकेशन को एक आइकन और setIndicator करने के लिए एक पाठ प्रदान करता है (सवाल से स्रोत के रूप में) चल रहा है, निम्नलिखित परीक्षा परिणाम था अपेक्षित आइकन के नीचे लेबल करें)

  • 4.0.3 के साथ फोन: आइकन नहीं दिखाए गए और केवल टेक्स्ट दिखाई दे रहा था।
  • गोली चल ICS: टैब आप भी पता चला के रूप में जहां आइकन और पाठ

के साथ दिखाया गया है, एक रिक्त स्ट्रिंग के साथ लेबल की जगहमाउस फोन पर दिखाई दिया - लेकिन फिर उपयोगकर्ता होगा आइकन के अर्थ का अनुमान लगाने की आवश्यकता है। इसके अलावा: पूर्व आईसीएस फोन पर ऐप के इस संस्करण को चलाने पर: टैब अपना आकार रखते हैं और छवि के नीचे एक खाली क्षेत्र छोड़ देते हैं।

इस डिवाइस कितनी जगह टैब मिलना चाहिए पर निर्णय संचालित बदलने के लिए, मैं इस tutorial on how to customize tabs में समाधान नहीं मिला:

सबसे पहले आप अपने स्वयं के टैब सूचक लेआउट उपलब्ध कराने की आवश्यकता ("लेआउट/tab_indicator.xml "ट्यूटोरियल में) ImageView और TextView समेत। फिर के माध्यम से इसका उपयोग करने के लिए TabSpec बताएं। और voilà: आपको आईसीएस के साथ फोन पर आइकन और लेबल भी मिलता है।

यहाँ (इस वर्तमान गतिविधि =) कैसे सूचक स्थापित करने के लिए पर महत्वपूर्ण हिस्सा है:

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); 

आप पुराने और नए फोन पर नजर का एक ही रंग-रूप को पसंद करते हैं यहाँ एक अतिरिक्त नजर है : http://jgilfelt.github.com/android-actionbarstylegenerator/। यह पृष्ठ टैब सहित एक्शन बार को कस्टमाइज़ करने के लिए छवियों और शैलियों को उत्पन्न करता है और मुझे पता लगाने में मदद करता है कि 9-पैच पृष्ठभूमि छवियों को कैसे परिभाषित करने की आवश्यकता है।

+2

शानदार, आपको ट्यूटोरियल बनाना चाहिए। पढ़ने और समझने के लिए वास्तविक आसान है। –

+0

इसके अलावा मैं अधिक अद्यतित टुकड़ों का उपयोग कर रहा हूं। पुरानी टैबएक्टिविटी के बजाय इसे टुकड़ों में कैसे कार्यान्वित करना है इसका कोई विचार? –

+2

टैब का चयन होने पर यह दृष्टिकोण छवि/पाठ के नीचे "पतली रेखा" भी ले जाता है। –

2

मैं निम्नलिखित तरीके से यह करने के लिए कोशिश करते हैं और यह ठीक काम करता है:

  1. setIndicator ("टैब") का उपयोग केवल पाठ सेट करने के लिए। आइकन सेट करने के लिए setBackgroundDrawable (-) का उपयोग कर

नमूना कोड:

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

इस के लिए एक और बहुत ही सरल समाधान है।

ऐसा लगता है कि यह मुद्दा नई होलो थीम और TabWidget में है। तो क्या आपको क्या करना चाहिए अपने values-v11 फ़ोल्डर में लेकिन पुराने TabWidget शैली इस तरह के साथ Holo विषय को परिभाषित है:

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

यह मेरे लिए काम करता है, आशा है कि यह किसी को मदद मिलती है।

+0

आपको यह लाइन ** मान-v14 ** और ऊपरी संस्करण फ़ोल्डर में भी जोड़नी होगी। – biswajitGhosh

7

आईसीएस में टेक्स्ट और आइकन दिखाने का यह एक आसान तरीका है। सेटअप Tabhost करने के बाद, मैं अगले तरीकों फोन:

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); 
} 

आप एक और अधिक उन्नत समाधान चाहते हैं, तो आप की तरह @sunadorer की सलाह देते हैं एक लेआउट बनाने के लिए की जरूरत है। मेरी सिफारिश यह है कि आप होलो थीम लेआउट का उपयोग करके अपना लेआउट बना सकते हैं, फ़ाइल को अपने sdk_dir/प्लेटफ़ॉर्म/एंड्रॉइड -14/डेटा/res/layout/tab_indicator_holo.xml पर फ़ाइल खोजें। अगले समारोह

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> 

अपनी गतिविधि या टुकड़ा में, बनाने के लिए::

मेरे उन्नत समाधान बगल में है, तो आप इस तरह एक लेआउट बनाने की जरूरत

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; 
} 

और आखिरकार जब आप अपनी गतिविधि या खंड में अपना टैब बनाते हैं तो अगला कोड उपयोग करें:

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

मैंने आईसीएस में इस समाधान का परीक्षण किया है और ठीक काम करता है।

गुड लक :)

+1

मुझे इस पंक्ति में त्रुटि मिल रही है एनपीई: छवि दृश्य टैब ImageView = (ImageView) tabHost.getTabWidget()। GetChildTabViewAt (tabIndex) .findViewById (android.R.id.icon); – user3233280

संबंधित मुद्दे