ViewPagerIndicator标签页:文本上方的图标

8

我正在使用ViewPagerIndicator,我想更改选项卡样式,使图标显示在文本上方,而不是默认的将图标放在左侧,标题放在右侧。


你好,有解决这个问题的方案吗? - Kid24
3个回答

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

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

TabPageIndicator.java 中找到

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

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

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

tabView.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);

如果你需要更灵活的东西,我想出了这些变化(仍在TabPageIndicator.java中),应该可以工作。这些变化已经在我的GitHub上复制,所以有一个可用的示例。
成员变量:
/**
* 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;
    }
}

在`addTab()`中,更改

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);

感谢您的回答和在Github上的工作副本,非常感谢!我担心通过库本身不可能实现。嗯,也许我会发送一个pull请求(除非你要做它:))。再次感谢。 - Bart Kiers
我刚意识到我还不能分配悬赏积分(只能在24小时后才能这样做):我会在我有能力时将它们分配给你和罗伯特。 - Bart Kiers
@BartKiers 没问题 :-) 我对代码进行了一些小改动,一旦我确认它正常工作,我就会发送一个拉取请求。 - A--C
不错,我看到了这个 pull request:https://github.com/JakeWharton/Android-ViewPagerIndicator/pull/232 请注意,我看到你是对 master 分支发起的 pull 请求,但应该改为 dev 分支。 - Bart Kiers
@BartKiers 看起来每个人的拉取请求都是针对主分支的,所以我想我会保持拉取请求不变 :-) - A--C
啊,我没注意到那个。我记得Jake Wharton曾经要求有人从dev分支上合并代码,但可能是在另一个仓库里。 - Bart Kiers

8

您可以通过修改ViewPageIndicator库中TabPageIndicator类内addTab方法中的第180行代码来实现此效果(至少在今天的代码版本28/05/2013中是这样的)。

原始文件如下:

180        tabView.setCompoundDrawablesWithIntrinsicBounds( iconResId, 0, 0, 0 );

如果您想让图标位于文本上方,则应将其修改为以下内容。

180        tabView.setCompoundDrawablesWithIntrinsicBounds( 0, iconResId, 0, 0 );

这是更改的截图: 在此输入图片描述 如你所料,通过调整不同参数中的iconResId,可以在文本周围的任何位置放置图标,方法是使用setCompoundDrawablesWithIntrinsicBounds

Robert,我刚刚把所有的分数都授予了A--C。我原以为我可以将这些分数分配到多个答案中,但显然我在创建赏金时并非如此。很抱歉!尽管如此,我仍然非常欣赏你的回答! - Bart Kiers

0

有一种更简洁的方法可以实现这个,而不需要修改库。只需将类TabPageIndicator复制并粘贴到您的项目中,并修改其他答案中指出的行。然后将该类重命名为您喜欢的任何名称,并将其用作TabPageIndicator


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接