我正在尝试使用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" />
结果如下:
您可以看到,即使选项卡的数量似乎正确,选项卡中也没有文本。难道我做错了什么?听说自定义选项卡视图中ID为TextView
的{{1}}会受到text1
调用的影响。为什么这里没有发生这种情况?