2014-03-31 52 views
7

我在我的應用程序中使用ShowCaseView庫。我想將「確定」按鈕移動到左側。 我用這個代碼(在庫中相同):Android ShowCaseView的buttonLayoutParams將不起作用

// 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); 

但它不工作?任何人都可以告訴問題在哪裏?

+0

您可以使用ViewGroup.LayoutParams.MATCH_PARENT更改ViewGroup.LayoutParams.WRAP_CONTENT並嘗試? – playmaker420

+1

@ playmaker420我試過,但沒有工作。 – Mehrdad

回答

17

我知道這個問題是很老,但萬一有人需要一個有效的解決方案:

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); 

這爲我工作,希望它可以幫助別人。

+0

這對我來說非常合適,謝謝! +1 –