有没有办法在 Showcase View 中设置标题和消息的位置?我看到该库使用函数来查找最佳位置,但我想覆盖它。
有没有办法在 Showcase View 中设置标题和消息的位置?我看到该库使用函数来查找最佳位置,但我想覆盖它。
我刚刚深入研究了ShowcaseView
的源代码,似乎没有内置的方法来设置文本位置。所以我对该库进行了自己的修改。以下是我所做的更改:
在ShowcaseView
类中添加了以下字段:
private boolean hasManualPostion = false;
private int xPosition, yPosition, width;
然后在ShowcaseView
类的内部类Builder
中添加以下方法:
public Builder hasManualPosition(boolean hasManualPosition) {
showcaseView.hasManualPostion = hasManualPosition;
return this;
}
public Builder xPostion(int xPostion) {
showcaseView.xPosition = xPostion;
return this;
}
public Builder yPostion(int yPostion) {
showcaseView.yPosition = yPostion;
return this;
}
接下来,将这个方法加入到TextDrawer
类中:
public void setTestPostionManually(int xPosition, int yPosition) {
mBestTextPosition[0] = xPosition;
mBestTextPosition[1] = yPosition;
}
最后,将 ShowcaseView
中的 onPreDraw
方法更改为以下内容:
@Override
public boolean onPreDraw() {
boolean recalculatedCling = showcaseAreaCalculator.calculateShowcaseRect(showcaseX, showcaseY, showcaseDrawer);
boolean recalculateText = recalculatedCling || hasAlteredText;
if (recalculateText) {
textDrawer.calculateTextPosition(getMeasuredWidth(), getMeasuredHeight(), this, shouldCentreText);
if (hasManualPostion) {
textDrawer.setTestPostionManually(xPosition, yPosition);
}
}
hasAlteredText = false;
return true;
}
ShowcaseView
:sv = new ShowcaseView.Builder(this, true)
.setTarget(target)
.setContentTitle(R.string.showcase_main_title)
.setContentText(R.string.showcase_main_message)
.setStyle(R.style.CustomShowcaseTheme2)
.setShowcaseEventListener(this)
.hasManualPosition(true)
.xPostion(0)
.yPostion(240)
.build();
享受吧!
CalculateTextOnPreDraw
类(现在包含 onPreDraw
方法)。现在,您只需将 this
替换为 ShowcaseView.this
,它就可以完美地工作了。 - Ashish Tanna