JakeWharton TitlePageIndicator:如何仅显示左右标题的一半

时间:2013-01-05 16:38:49

标签: android viewpagerindicator

我正在使用JakeWharton TitlePageIndicator。该指标一直很有效,直到我将标题设置为一个有点长的字符串(20个字符),中心标题显示得很好,但左右标题太靠近中心标题。是否有任何方法可以像在Google Play中那样只显示左侧和右侧标题的一半(或修复长度)?

谢谢,

2 个答案:

答案 0 :(得分:2)

对于任何可能遇到同样问题的人。在JakeWharton TitlePageIndicator中,有2种方法可以控制左右标题的显示:

/**
 * Set bounds for the right textView including clip padding.
 *
 * @param curViewBound
 *            current bounds.
 * @param curViewWidth
 *            width of the view.
 */
private void clipViewOnTheRight(Rect curViewBound, float curViewWidth, int right) {
    // isShowPartOfInactiveTitles is my variable which is set default to TRUE
    if(isShowPartOfInactiveTitles){
        curViewBound.right = (int)(right + curViewWidth - 60);
        curViewBound.left = (int)(right - 60);
    }else{
        curViewBound.right = (int) (right - mClipPadding);
        curViewBound.left = (int) (curViewBound.right - curViewWidth);
    }
}

/**
 * Set bounds for the left textView including clip padding.
 *
 * @param curViewBound
 *            current bounds.
 * @param curViewWidth
 *            width of the view.
 */
private void clipViewOnTheLeft(Rect curViewBound, float curViewWidth, int left) {
    // isShowPartOfInactiveTitles is my variable which is set default to TRUE
    if(isShowPartOfInactiveTitles){
        curViewBound.left = (int)(left - curViewWidth + 60);
        curViewBound.right = (int)(left + 60);
    }else{
        curViewBound.left = (int) (left + mClipPadding);
        curViewBound.right = (int) (mClipPadding + curViewWidth);   
    }
}

上面的代码片段只会显示60dp的左右标题。您可以将此数字设为属性并将其传递给xml。在这里,我将其硬编码以便于理解。

答案 1 :(得分:2)

您不需要像这样修改库。 只需为TitlePageIndicator指定clipPadding即可。

就像那样:

mIndicator.setClipPadding(-60);