TabWidget - How to set the position of indicator text?

I am trying to use TabHost and TabWidget in my android app. This is my main.xml layout:

<TabHost android:id="@+id/mainTabHost" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="65px"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/contentTags" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> </LinearLayout> <LinearLayout android:id="@+id/contentPreferences" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> </LinearLayout> </FrameLayout> </TabHost> 

And my code is:

 final TabHost mainTabHost = (TabHost) this.findViewById(R.id.mainTabHost); mainTabHost.setup(); final TabHost.TabSpec tabSpecTags = mainTabHost.newTabSpec("tabTags"); tabSpecTags.setIndicator(this.getString(R.string.tab_name_tags)); tabSpecTags.setContent(R.id.contentTags); mainTabHost.addTab(tabSpecTags); final TabHost.TabSpec tabSpecPreferences = mainTabHost.newTabSpec("tabPreferences"); tabSpecPreferences.setIndicator(this.getString(R.string.tab_name_preferences)); tabSpecPreferences.setContent(R.id.contentPreferences); mainTabHost.addTab(tabSpecPreferences); 

The problem is that I don't want my tabs to be so tall (65 pixels). However, if I set layout_height TabWidget, for example, 30px, I do not see tab shortcuts (indicator) on tabs at all.

It seems that there is a minimum required height for TabWidget (65px?), And the indicator is located at the bottom of this 65px?

Is there a way to adjust the positioning of the indicator?

Thanks!

+4
source share
4 answers

However, if I set layout_height TabWidget, for example. 30px, I can’t see tab shortcuts (indicator) on tabs in general.

Note that the methods for this will not necessarily be reliable in future releases of Android if they change the way TabHost built.

Is there a way to adjust the positioning of the indicator?

Starting with API level 4 (aka, Android 1.6), TabHost.TabSpec accepts View as an indicator via setIndicator() , I have not tried it, but it can give you more control over the layout of a separate tab indicator.

+4
source

I see ... when u addTab, we usually use setIndicator as follows: QTabHost.addTab (QTabHost.newTabSpec ("tab_test2"). SetIndicator ("TAB 2"). Bla bla ....

u can use TextView to replace "TAB 2", become the following:

tview = new TextView (this); tview.setText ("Name is here"); QTabHost.addTab (QTabHost.newTabSpec ("tab_test2"). SetIndicator (tview) .bla bla ....

all u need just modifies the text view. Thanks ... ^^

+2
source
 int iH = getTabWidget().getLayoutParams().height; for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = iH; } 
+1
source
 for (int i = 0; i < 4; i++) { tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = 50; } 

using this we can do very easily

0
source

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


All Articles