我有這個在我的MainActivity.java:在這種情況下,我應該在哪裏放置ShowCaseView構建器?
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupHomeScreen();
}
public void setupHomeScreen() {
File latestPic = getMostRecentSnappiePicture();
if(latestPic != null){
//display pic
LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackground(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
else{
layout.setBackgroundDrawable(Drawable.createFromPath(latestPic.getAbsolutePath()));
}
//hide tutorial
findViewById(R.id.howitworks).setVisibility(View.INVISIBLE);
}
else{
//show tutorial
findViewById(R.id.howitworks).setVisibility(View.VISIBLE);
new ShowcaseView.Builder(this)
.setTarget(new ActionViewTarget(this, ActionViewTarget.Type.HOME))
.setContentTitle("ShowcaseView")
.setContentText("This is highlighting the Home button")
.hideOnTouchOutside()
.build();
}
}
}
正如你所看到的,在的onCreate,其文件是否存在調用setupHomeScreen和檢查。如果它不存在,它將顯示教程「howitworks」佈局圖像以及構建展示視圖。
所以這一切都很好,很好。在showcaseView仍然存在的情況下嘗試離開活動時,唯一的問題是,或者有時甚至在您退出展示視圖並嘗試啓動新活動後,此錯誤出現:ShowcaseView - width and height must be > 0
正如您在解答,解決的辦法是隻在回調創建展示查看原始視圖建立像這樣經過:
someView.post(new Runnable() {
@Override
public void run() {
// my ShowcaseView builder here
}
});
唯一的一點是,我不知道往哪裏放這個,因爲我的展示視圖應僅當getMostRecentSnappiePicture()中的文件爲空時才顯示。那麼我怎麼能把視圖創建回調放到我的邏輯中來檢查那個文件是否爲空?