Why does the Host tab not show the icon in android?

Here is the code for setting tabhost, however there are two problems

  • The text will go to the next line if it is too long, is it possible to reduce the size and force it into one line?
  • All icons are not displayed, even I'm sure the src image is correct.

    public class MainActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    
            tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
            tabHost.addTab(tabHost.newTabSpec("restaurant").setIndicator("Restaurant",getResources().getDrawable(R.drawable.food)),PlaceList.class, null);
            tabHost.addTab(tabHost.newTabSpec("attraction").setIndicator("Attraction",getResources().getDrawable(R.drawable.view)), PlaceList.class, null);
            tabHost.addTab(tabHost.newTabSpec("map").setIndicator("Map",getResources().getDrawable(R.drawable.map)),Map.class,null);
            tabHost.addTab(tabHost.newTabSpec("planner").setIndicator("Planner",getResources().getDrawable(R.drawable.plan)),Planner.class, null);
        }
     }
    
+4
source share
1 answer

TabSpec.setIndicator, Drawable, , null . , TextView , TabWidget.getTabCount, TabWidget.getChildTabViewAt View.findViewById, TextView, . TextView.setSingleLine.

    final TabWidget tabWidget = tabHost.getTabWidget();
    for (int i = 0; i < tabWidget.getTabCount(); i++) {
        final View tab = tabWidget.getChildTabViewAt(i);
        final TextView title = (TextView) tab.findViewById(android.R.id.title);
        title.setSingleLine();
    }

, Widget.TabWidget.

<style name="Your.TabWidget" parent="@android:style/Widget.TabWidget">
    <item name="android:tabLayout">@layout/your_tab_layout</item>
</style>

item android:tabWidgetStyle, .

+5

Source: https://habr.com/ru/post/1534113/


All Articles