2014-01-04 3 views
-2

Я новичок в android. Я сейчас тестирую TabView с некоторым текстом и изображением. Изображение показывается, но текст нет. Версия Android SDK - 4.4.3. Помогите мне найти это, пожалуйста. Я смотрел на многие другие места, но ничто из этого не работает для меня.Вкладка с текстом и изображением не отображает текст

Это код:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TabHost tabHost =(TabHost) findViewById(R.id.tabHost); 
    tabHost.setup(); 


    TabHost.TabSpec spec=tabHost.newTabSpec("mitab1"); 
    spec.setContent(R.id.tab1); 
    //spec.setIndicator("",getResources().getDrawable(android.R.drawable.ic_menu_rotate)); 
    spec.setIndicator(prepareTabView("ABCD", R.drawable.talk)); 


    TabHost.TabSpec spec1=tabHost.newTabSpec("mitab1"); 
    spec1.setContent(R.id.tab2); 
    spec1.setIndicator(prepareTabView("CDEF", R.drawable.ball)); 



    tabHost.addTab(spec); 
    tabHost.addTab(spec1); 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 


private View prepareTabView(String text, int resId) { 
    View view = LayoutInflater.from(this).inflate(R.layout.tabcustom, null); 
    ImageView iv = (ImageView) view.findViewById(R.id.TabImageView); 
    TextView tv = (TextView) view.findViewById(R.id.TabTextView); 
    iv.setImageResource(resId); 
    tv.setText(text); 
    return view; 

}

Это tabcustom.xml:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/TabLayout" 
android:padding="5dip" 
android:gravity="center" 
android:orientation="vertical" > 


    <ImageView 
     android:id="@+id/TabImageView" 
     android:src="@drawable/football" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <TextView 
     android:id="@+id/TabTextView" 
     android:text="Text" 
     android:paddingTop="5dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     /> 

+0

Вы можете оставить R.layout.tabcustom XML файл макета ...? –

+0

см. Http://stackoverflow.com/questions/8269356/i-want-to-set-image-and-also-text-on-tab –

+0

@ Gopal Rao => уже отредактирован :) –

ответ

0

ориентации компоновщика является Vertical вот почему текст будет показано в нижней части вкладки & изменить цвет текста, если вкладка b Цвет фона такой же, как цвет текста. Вы можете использовать нижеприведенный код для отображения центрального текста с фоновым изображением.

Изменение XML:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/TabLayout" 
android:padding="5dip" 
android:gravity="center" 
android:orientation="vertical" > 


    <TextView 
     android:id="@+id/TabTextView" 
     android:text="Text" 
     android:paddingTop="5dip" 
     android:layout_width="fill_parent" 
     android:gravity="center" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     /> 

Класс:

private View prepareTabView(String text, int resId) { 
    View view = LayoutInflater.from(this).inflate(R.layout.tabcustom, null); 
    LinearLayout iv = (LinearLayout) view.findViewById(R.id.TabLayout); 
    TextView tv = (TextView) view.findViewById(R.id.TabTextView); 
    iv.setImageResource(resId); 
    tv.setText(text); 
    return view;} 
+0

сэр Пожалуйста, ответьте на этот вопрос [ссылка] (http://stackoverflow.com/questions/20988505/open-child-activity-in-tab-host-android) –

Смежные вопросы