有没有办法在Showcase视图中设置Title和Message的位置?我看到库使用函数来找到最佳位置,但我想覆盖它。
答案 0 :(得分:8)
我刚刚进入ShowcaseView
源代码,似乎没有办法设置文本位置。所以我在库中添加了自己的修改。以下是我所做的更改:
在课程ShowcaseView
中添加了以下字段:
private boolean hasManualPostion = false;
private int xPosition, yPosition, width;
然后在Builder
类ShowcaseView
类的内部类中,添加以下方法:
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;
}
并在onPreDraw
中更改ShowcaseView
方法,如下所示:
@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();
享受!