[英]How to set particular Tab and get Current Tab while using TabLayout of android.support.design.widget.TabLayout;
[英]How to use android.support.design.widget.TabLayout to create custom layout of tab?
我找到了以下有關如何設置customView
。 但是,當我將布局中的id
更改為“ text1”和“ icon”時, setText()
和setIcon()
不起作用。
公共TabLayout.Tab setCustomView(int layoutResId)
設置要用於此選項卡的自定義視圖。
如果膨脹的布局包含ID為
text1
的TextView
,則將使用setText(CharSequence)
的值進行更新。 同樣,如果此布局包含帶有ID圖標的ImageView
,則它將使用setIcon(Drawable)
的值進行更新。
(來源: http : //developer.android.com/reference/android/support/design/widget/TabLayout.Tab.html#setCustomView ( int) )
誰能給我一個例子呢?
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/text1
和@android:id/icon
。
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@android:id/icon"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:id="@android:id/text1"
android:gravity="center"
android:layout_below="@android:id/icon" />
如果您需要在代碼中引用這些ID,則它們將為android.R.id.text1
和android.R.id.icon
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.