簡體   English   中英

如何使用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為text1TextView ,則將使用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.text1android.R.id.icon

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM