如何使pagerSlidingTabstrip中的标题动态化?

时间:2016-02-16 06:26:54

标签: android android-viewpager pagerslidingtabstrip

我需要根据寻呼机中子片段中动态变化的项目来更改寻呼机中的标题。我试过这种方式

String CREATE_CategoryS_TABLE = "CREATE TABLE " + TABLE_CategoryS + "("
        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_IMG + " INTEGER," +
        KEY_NAME + " TEXT," + KEY_SELECTION + " TEXT"  + ")";

但它似乎没有起作用。 提前致谢

3 个答案:

答案 0 :(得分:2)

您可以使用它来访问每个标签的文本视图

    LinearLayout mTabsLinearLayout = ((LinearLayout) mPagerSlidingTabStrip.getChildAt(0));
    for (int i = 0; i < mTabsLinearLayout.getChildCount(); i++)
    {
        TextView tv = (TextView) mTabsLinearLayout.getChildAt(i);
        tv.setText("title");
    }

现在你想要特定标签的文本视图

LinearLayout mTabsLinearLayout = ((LinearLayout) mPagerSlidingTabStrip.getChildAt(0));
TextView tv = (TextView) mTabsLinearLayout.getChildAt(<perticular tab position>);
tv.setText(yourText);

答案 1 :(得分:1)

以下演示代码也有助于动态更改标签标题:

private LinearLayout mTabsLinearLayout;
public void setUpTabStrip(int position)
  {
    this.mTabsLinearLayout = ((LinearLayout)this.tabsStrip.getChildAt(0));
for (int i=0;i<mTabsLinearLayout.getChildCount();i++)
{
  TextView tempTextView = (TextView)this.mTabsLinearLayout.getChildAt(i);
  if (i == position) {
    tempTextView.setText("Your Text");
  }
 }
} 

答案 2 :(得分:0)

尝试以下方法。您也可以根据自己的要求进行更改。

public void setTabText(int tabPosition, String text) {
   LinearLayout mTabsLinearLayout = ((LinearLayout) mPagerSlidingTabStrip.getChildAt(0));
     for (int i = 0; i < mTabsLinearLayout.getChildCount(); i++) {
        TextView tv = (TextView) mTabsLinearLayout.getChildAt(i);
        if(i == tabPosition) {
        tv.setText(text);
     }
   }
}