Android ShowCaseView的buttonLayoutParams不起作用。

7

我正在使用ShowCaseView库来开发我的应用程序。我想将“确认”按钮移动到左侧。 我使用了与库中相同的代码:

// The following code will reposition the OK button to the left.
RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
lps.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lps.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
int margin = ((Number) (getResources().getDisplayMetrics().density * 12))
        .intValue();
lps.setMargins(margin, margin, margin, margin);
View showcasedView = findViewById(R.id.ib_next);
ViewTarget target = new ViewTarget(showcasedView);
ShowcaseView.ConfigOptions co = new ShowcaseView.ConfigOptions();
co.buttonLayoutParams = lps;
co.fadeInDuration = 1000;
co.fadeOutDuration = 1000;
ShowcaseView sv = ShowcaseView.insertShowcaseView(target, this,
        R.string.showcase_title, R.string.showcase_details, co);

但是它不工作?有人能告诉我问题出在哪里吗?

你能否将 ViewGroup.LayoutParams.WRAP_CONTENT 更改为 ViewGroup.LayoutParams.MATCH_PARENT 并尝试一下? - playmaker420
1
@playmaker420 我尝试了,但没有成功。 - Mehrdad
1个回答

22

我知道这个问题已经相当老了,但是万一有人需要一个可行的解决方案:

RelativeLayout.LayoutParams lps = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// This aligns button to the bottom left side of screen
lps.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lps.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Set margins to the button, we add 16dp margins here
int margin = ((Number) (getResources().getDisplayMetrics().density * 16)).intValue();
lps.setMargins(margin, margin, margin, margin);

ViewTarget target = new ViewTarget(R.id.buttonBlocked, this);
sv = new ShowcaseView.Builder(this)
        .withMaterialShowcase()
        .setTarget(target)
        .setContentTitle(R.string.showcase_main_title)
        .setContentText(R.string.showcase_main_message)
        .build();
// Set declared button position to ShowcaseView
sv.setButtonPosition(lps);

这对我有用,希望能帮到别人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接