2012-06-09 63 views
8

Tôi đang cố gắng thực hiện một ứng dụng tab đơn giản trong Android với hai tab. Vấn đề của tôi là khi tôi đặt mã này, trong tab, chỉ hiển thị văn bản, nhưng không có biểu tượng. Nếu tôi đặt văn bản vào "" biểu tượng được hiển thị.Biểu tượng tab không hiển thị

Ai đó có thể giúp tôi không? Phiên bản Android của tôi là 4.0.3.

Cảm ơn rất nhiều.

<?xml version="1.0" encoding="utf-8"?> 

<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:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

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

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

     <LinearLayout android:id="@+id/tab1" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      <TextView android:id="@+id/textView1" 
       android:text="Contenido Tab 1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
    </LinearLayout> 

     <LinearLayout android:id="@+id/tab2" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 
      <TextView android:id="@+id/textView2" 
       android:text="Contenido Tab 2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
    </LinearLayout> 

    </FrameLayout> 
</LinearLayout> 
</TabHost> 

và hệ thống mã ngành là

public class TabTestActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    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("sss", 
      res.getDrawable(android.R.drawable.ic_btn_speak_now)); 
    tabs.addTab(spec); 

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



    tabs.setCurrentTab(0); 
} 

như bạn có thể thấy là rất đơn giản. Nhưng khi tôi viết spec.setIndicator("", res.getDrawable(android.R.drawable.ic_dialog_map)); Tôi có thể thấy biểu tượng, bu khi tôi viết spec.setIndicator("TAB2", res.getDrawable(android.R.drawable.ic_dialog_map)); Tôi chỉ có thể thấy TAB2, nhưng không có cả hai.

Dường như không có không gian khó khăn nào để hiển thị cả hai. Vì vậy, tôi đã cố gắng tăng chiều cao của tab bằng cách này

tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 150; 

nhưng dường như không hoạt động.

Trả lời

4

// Are qua tải một 1st, do đó bạn có thể thấy chỉ thêm vào cuối cùng một

TabHost.TabSpec spec=tabs.newTabSpec("mitab1"); 

     spec.setIndicator("sss", 
       res.getDrawable(android.R.drawable.ic_btn_speak_now)); 
Intent sssIntent = new Intent(this, First.class); 
spec.setContent(sssIntent); 
     tabs.addTab(spec); 

TabHost.TabSpec spec2=tabs.newTabSpec("mitab2"); 
     spec2=tabs.newTabSpec("mitab2"); 
     spec2.setIndicator("TAB2", 
       res.getDrawable(android.R.drawable.ic_dialog_map)); 
Intent sssIntent2 = new Intent(this, Second.class); 
spec2.setContent(sssIntent2); 
     tabs.addTab(spec2); 
+0

bạn có thể giải thích thêm một chút không? Tôi không hiểu khi bạn nói quá tải? cảm ơn rất nhiều – theholy

+0

Điều này không làm việc cho tôi –

8

tôi thay tên nhãn với giá trị null. Bây giờ tôi có thể thấy biểu tượng một mình .. Không thể tìm ra giải pháp nào khác.

TabHost.TabSpec spec=tabs.newTabSpec("mitab1"); 

spec.setIndicator("", 
        res.getDrawable(android.R.drawable.ic_btn_speak_now)); 
Intent sssIntent = new Intent(this, First.class); 
spec.setContent(sssIntent); 
tabs.addTab(spec); 
+0

U r phải .. chúng tôi phải xóa nhãn của tab để xem biểu tượng ... đó là ... Bằng cách này Cảm ơn bạn đã giúp đỡ bạn thân – Noman

+0

nếu vị trí null tôi có thể thấy .. nhưng không thể thấy văn bản biểu tượng bên dưới ... có thể cho biết tôi muốn cả biểu tượng và văn bản hiển thị ... –

2

Khả năng hiển thị biểu tượng (cùng với nhãn) trong tab phụ thuộc vào thiết bị đích và phiên bản nền tảng Android.

Tôi đã xem xét kỹ hơn vấn đề này và thêm chi tiết và giải pháp cho câu hỏi khác của bạn (khá tương tự) về vấn đề này; Nó có thể được tìm thấy ở đây:

https://stackoverflow.com/a/11379708/414581

0

Thêm này trong AndroidManifest.xml giải quyết vấn đề này.

<application 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" 
android:theme="@android:style/Theme.NoTitleBar"> 
</application> 
Các vấn đề liên quan