【问题标题】:How to create a TabLayout with SlidingTabStrip placed at top of view and not at bottom如何使用放置在视图顶部而不是底部的 SlidingTabStrip 创建 TabLayout
【发布时间】:2015-10-04 17:39:06
【问题描述】:

使用 Android 支持设计库我想将 TabLayout.SlidingTabStrip 位置从底部更改为顶部。问题是 SlidingTabStrip 是私有类,所以我找不到很好的解决方案来扩展它并覆盖它的 draw 方法。

这是默认设置:

这就是我想要的:

所以为了简化我想改变第二行这个方法:

public void draw(Canvas canvas) {
    super.draw(canvas);
    if(this.mIndicatorLeft >= 0 && this.mIndicatorRight > this.mIndicatorLeft) {
        canvas.drawRect((float)this.mIndicatorLeft, (float)(this.getHeight() - this.mSelectedIndicatorHeight), (float)this.mIndicatorRight, (float)this.getHeight(), this.mSelectedIndicatorPaint);
    }
}

从此:

canvas.drawRect((float)this.mIndicatorLeft, (float)(this.getHeight() - this.mSelectedIndicatorHeight), (float)this.mIndicatorRight, (float)this.getHeight(), this.mSelectedIndicatorPaint);

进入这个方法:

canvas.drawRect((float)this.mIndicatorLeft, 0f, (float)this.mIndicatorRight, (float)this.selectedIndicatorHeight, this.mSelectedIndicatorPaint);

我怎样才能做到这一点? 谢谢。

【问题讨论】:

  • 目前我看不出有什么方法可以做到这一点。您可能只需要在指示器上设置一个透明颜色,然后在 TabLayout 顶部放置一个自定义指示器:/
  • 目前我想出的最佳实现只是从支持库中复制实现并重写 SlidingTapStrip 的绘制方法,使其适合我的需要。它有效,但这是一种不好的方法。

标签: java android androiddesignsupport android-tablayout


【解决方案1】:

使用 android.support.design.widget.TabLayout 并覆盖其 onDraw(Canvas canvas)

【讨论】:

  • 这不起作用,因为它在 TabLayout 的内部私有类 SlidingTabStrip 中,实际的 draw(..) 实现驻留。
猜你喜欢
  • 1970-01-01
  • 2020-11-06
  • 2017-06-19
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
相关资源
最近更新 更多