问题描述
我使用的是 ViewPagerIndicator 我想改变,而不是默认值,即左侧放置图标和标题右侧的标签样式,以便我能得到上面的文字图标。
I'm using the ViewPagerIndicatorand I would like to change the tab style so I could get icons above text, instead of the default, which places the icon on the left side, and the title to the right.
推荐答案
为什么图标总是显示在左侧的原因是因为这片code的:
The reason why the icons always appear on the left is because of this piece of code:
if (iconResId != 0) {
tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}
在<$c$c>TabPageIndicator.java$c$c>
这是一个私有方法的一部分( addTab()
),因此,不能在没有修改库本身进行修改。
This is part of a private method (addTab()
), and so, cannot be changed without modifying the library itself.
幸运的是,这是不是太难的事。请确保你已经下载了 ViewPagerIndicator
源$ C $ C,然后打开 TabPageIndicator.java
Luckily, this isn't too hard to do. Make sure that you have the ViewPagerIndicator
source code downloaded, then open up TabPageIndicator.java
如果你想永久改变的位置(如永久性的,你可以与源$ C $ c修改得到),更改 iconResId
的位置setCompoundDrawablesWithIntrinsicBounds()方法。例如,将图标上方需要 iconResId
是第二个参数的方法。
If you want to change the location permanently (as permanent as you can get with a source code change), change the location of iconResId
in the setCompoundDrawablesWithIntrinsicBounds() method. For example, placing the icons at the top needs iconResId
to be the second argument to the method.
tabView.setCompoundDrawablesWithIntrinsicBounds(0, iconResId, 0, 0);
如果你需要的东西多一点灵活的,我想出了这些变化(仍然是 TabPageIndicator.java
)应该工作。这些变化已经反映在我的GitHub ,所以有工作的例子。
If you need something a little more flexible, I came up with these changes (still in TabPageIndicator.java
) that should work. These changes have been mirrored on my GitHub, so there is a working example.
成员变量:
/**
* 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]);
}
非库实现(从提供的示例code取)
Non-library implementation (taken from the sample code provided)
TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
indicator.setTabIconLocation (TabPageIndicator.LOCATION_UP);
indicator.setViewPager(pager);
这篇关于ViewPagerIndicator标签:图标上面的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!