我發現瞭如何設置customView
的以下文檔。但是,當我將佈局中的id
更改爲「text1」和「圖標」時,setText()
和setIcon()
不起作用。如何使用android.support.design.widget.TabLayout來創建標籤的自定義佈局?
公共TabLayout.Tab setCustomView(INT layoutResId)
設置自定義視圖要用於該選項卡。
如果充氣佈局包含ID爲
text1
的TextView
,那麼將使用setText(CharSequence)
給出的值進行更新。同樣,如果此佈局包含帶有ID圖標的ImageView
,則它將使用給予setIcon(Drawable)
的值進行更新。
誰能給我這是如何工作的例子嗎?
Java代碼:
TabLayout.Tab tabAdd = tabLayout.getTabAt(0);
tabAdd.setCustomView(R.layout.tab_layout_custom_view);
tabAdd.setText("Add");
tabAdd.setIcon(R.mipmap.add_tab).setText("Add");
佈局代碼:
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:id="@+id/text1"
android:gravity="center"
android:layout_below="@+id/icon" />
值得一讀。 [Android中'@ + id','@ id'和'@ android'之間的區別](http://android-wtf.com/2012/11/difference-between-at-plus-id-and-at- id-in-android /) –
您可以選擇使自己管理的自定義視圖充滿膨脹,在這裏查看完整的實現:http://stackoverflow.com/a/32547335/4409409 –