2016-06-20 215 views
0

退出應用程序並重新進入時出現奇怪的錯誤,ShowcaseView未居中,我在片段中使用此代碼。 這是我的代碼:ShowcaseView不居中

target = new ViewTarget(iv_QR); 
      if (showcase == null) { 
       Button button = new Button(getContext()); 
       button.setText("Got it !"); 
       button.setEnabled(false); 
       button.setVisibility(View.INVISIBLE); 
       showcase = new ShowcaseView.Builder(getActivity()) 
         .setStyle(R.style.CustomShowcaseTheme2) 
         .replaceEndButton(button) 
         .setTarget(target) 
         .withMaterialShowcase() 
         .setContentTitle("QR Scan(Required)") 
         .setContentText("Please press the QR icon to scan the QR") 
         .build(); 

,這是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".Fragments.QRFragment"> 


    <ImageView 
     android:id="@+id/iv_QR" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@mipmap/qricon" /> 
</RelativeLayout> 

我附上的圖片: enter image description here

+0

你在'RelativeLayout'中嘗試過'android:gravity =「center」嗎?在這種情況下,你可以刪除'android:layout_centerHorizo​​ntal'和'layout_ centerVertical'。 – REG1

+0

它不會幫助@ REG1 –

+0

當應用程序第一次啓動它居中,但是當我退出並再次輸入這個問題發生 –

回答

0

所以假定問題是由ShowcaseView造成正在創建在onCreateView()已返回之前,請考慮在您的Fragment的onActivityCreated()方法中創建並顯示ShowcaseView

Here is some sample codeShowcaseView與Fragments一起使用,請嘗試在您自己的代碼中實現它。正如你所看到的,它也使用方法onActivityCreated()

+0

不幸的是,即使當我有視圖@ REG1時,它也沒有幫助 –

+0

您能發佈更多代碼(包括更新)嗎?即顯示您實例化ShowcaseView的所有方法以及與之相關的所有其他代碼? – REG1

+0

這就是很多代碼,我會嘗試更好地解釋我的自我:我使用AppIntro 3片段,我用第一個片段的展示案例,一切都很好,當我完成我想要移動到的第一個片段時下一個片段(你在圖片中看到的那個),使用onActivityCreated()時的問題它會立即顯示展示,因爲showcase.build()顯示它。現在展示的是另一個@ REG1 –