I'm trying to create a tab layout with custom tabs in android:
FnBFragment.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_fnb, container, false);
mTabs = (TabLayout) view.findViewById(R.id.tabs);
Resources res = getResources();
int customTabViewId = R.layout.fnb_tab_view;
for (String category : categories) {
TabLayout.Tab tab;
View tabView = inflater.inflate(customTabViewId, container, false);
tab = mTabs.newTab();
tab.setCustomView(tabView);
tab.setText("Hello"); // just for testing, should be category instead of "Hello"
mTabs.addTab(tab);
}
}
fnb_tab_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="20sp"
android:textColor="@color/black"/>
</LinearLayout>
fragment_fnb.xml:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" />
Here's the result:
As you can see there is no text in the tabs, even though the number of tabs seems to be correct. Am I doing something wrong? I heard that the TextView
in the custom tab view with id text1
will be affected by the setText()
call. Why doesn't this happen here?