Android ViewPagerIndicator选项卡:文本上方的图标

Android ViewPagerIndicator选项卡:文本上方的图标,android,icons,viewpagerindicator,Android,Icons,Viewpagerindicator,我在用电话 我想改变标签样式,这样我就可以在文本上方获得图标,而不是默认设置,默认设置将图标放在左侧,标题放在右侧 只需修改ViewPageIndicator库源代码中的一行即可实现此效果 要修改的行位于addTab方法中TabPageIndicator类中的编号180(至少在当前版本的代码28/05/2013上) 原始文件是 180 tabView.setCompoundDrawablesWithIntrinsicBounds( iconResId, 0, 0, 0 ); 如果

我在用电话
我想改变标签样式,这样我就可以在文本上方获得图标,而不是默认设置,默认设置将图标放在左侧,标题放在右侧

只需修改ViewPageIndicator库源代码中的一行即可实现此效果

要修改的行位于
addTab
方法中
TabPageIndicator
类中的编号
180
(至少在当前版本的代码28/05/2013上)

原始文件是

180        tabView.setCompoundDrawablesWithIntrinsicBounds( iconResId, 0, 0, 0 );
如果希望图标位于文本顶部,则应将其修改为以下内容

180        tabView.setCompoundDrawablesWithIntrinsicBounds( 0, iconResId, 0, 0 );
这是一个改变的截图


正如您现在可能猜到的,通过在
setCompoundDrawablesWithIntrinsicBounds
方法的不同参数中使用iconResId,您可以将图标放在文本周围的任何位置。

图标总是出现在左侧的原因是因为这段代码:

if (iconResId != 0) {
     tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}
发现于

这是私有方法(
addTab()
)的一部分,因此在不修改库本身的情况下无法更改

幸运的是,这并不难做到。确保下载了
ViewPagerIndicator
源代码,然后打开
TabPageIndicator.java

如果要永久更改位置(只要更改源代码即可永久更改),请在方法中更改
iconResId
的位置。例如,将图标放在顶部需要
iconResId
作为该方法的第二个参数

tabView.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
如果您需要更灵活一点的东西,我提出了这些更改(仍在
TabPageIndicator.java
)应该可以使用。这些更改已被镜像,因此有一个工作示例

成员变量:

/**
* Constants to improve readability - no magic numbers.
*/
public final static int LOCATION_LEFT =0;
public final static int LOCATION_UP = 1;
public final static int LOCATION_RIGHT = 2;
public final static int LOCATION_BOTTOM =3;

/**
* Stores the location of the tab icon
*/
private int location = LOCATION_LEFT;

/**
* Used to store the icon.
*/
private int [] drawables = new int [4];

/**
 * Holds the value used by setCompoundDrawablesWithIntrinsicBounds used to denote no icon.
 */
private static int NO_ICON = 0;
添加此方法:

public void setTabIconLocation (int newLocation){
    if (location > LOCATION_BOTTOM || location < LOCATION_LEFT)
        throw new IllegalArgumentException ("Invalid location");
    this.location = newLocation;
    for (int x = 0; x < drawables.length;x++){
        drawables [x] = NO_ICON;
    }
}

非库实现(取自提供的示例代码)


有一种更简洁的方法可以做到这一点,而无需修改库。只需将该类复制并粘贴到您的项目中,并修改其他答案中指出的行。然后将该类重命名为您喜欢的名称,并将其作为
选项卡页指示器使用

嗨,这个问题有什么解决方案吗?谢谢您的回答和Github上的工作副本,非常感谢!我担心通过图书馆本身无法做到这一点。啊,好吧,也许我会感兴趣的拉请求(除非你要这样做:)。再次感谢。我刚刚意识到我还不能分配赏金点数(只能在24小时后分配):我会在我有能力的时候把它们分给你和罗伯特。@BartKiers没问题:-)我对代码做了一些小的更改,一旦我确认它工作正常,我会发送一个拉取请求。很好,我看到了拉取请求:注意,我看到您拉取了
master
分支,它可能应该是
dev
分支。@BartKiers似乎每个人的拉取请求都是针对master的,所以我想我会保持拉取请求的原样:-)Robert,我刚刚把所有的分数都给了A--C。我想我可以把分数分成多个答案,但显然不是我创造赏金的方式。很抱歉尽管如此,我们还是很感激你的回答!
if (iconResId != 0) {
     tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}
if (iconResId != 0) {
    drawables [location] = iconResId;
    tabView.setCompoundDrawablesWithIntrinsicBounds(drawables[0], drawables[1], drawables[2], drawables[3]);
}
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setTabIconLocation (TabPageIndicator.LOCATION_UP);
indicator.setViewPager(pager);