2017-06-04 48 views
0

一個奇怪的問題,我有一個佈局,作爲AlertDialog在我的Android應用程序中打開,但由於某種原因,只顯示xml文件中的兩個LinearLayouts的第二個。我沒有意識到佈局有可能部分忽略其視圖。奇怪的是,如果我使用PopupWindow而不是AlertDialog,則佈局繪製正確,所以我認爲導致問題的兩者之間存在一些差異。Android AlertDialog不顯示整個XML

有問題的XML佈局如下。它的根源是一個帶有兩個孩子的LinearLayout,其權重分別爲0.10和0.90。繪製時,重量爲0.90的佈局填充整個父級,並且無法找到重量爲0.10的佈局。改變這些重量值無法彌補這一點。

什麼是修復?

menu_popup_set_chords.xml

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_height="match_parent" 
android:layout_width="match_parent"> 

<!-- This is the missing Layout on draw --> 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:orientation="vertical" 
    android:layout_weight="0.10" 
    android:background="#000000"> 
</LinearLayout> 

<!-- This layout fills the entire dialog window instead of 90% of it --> 
<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:background="#212121" 
    android:layout_weight="0.90"> 

    <LinearLayout 
     android:layout_weight="1.40" 
     android:layout_width="0dp" 
     android:layout_height="match_parent"> 

     <!-- Custom view for Canvas --> 
     <com.example.foo.CustomChordCanvas 
      android:id="@+id/chordCanvas" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/neck_hud_background_alpha85"/> 

    </LinearLayout> 

    <LinearLayout 
     android:orientation="vertical" 
     android:padding="8dp" 
     android:layout_weight="3.60" 
     android:layout_width="0dp" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3.10"> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/rv_userChords" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"/> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="0.5"> 

      <LinearLayout 
       android:id="@+id/fl_actionButtons" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center" 
       android:orientation="horizontal"> 

       <LinearLayout 
        android:layout_weight="0.5" 
        android:layout_width="0dp" 
        android:layout_height="match_parent"> 

        <android.support.v7.widget.AppCompatImageButton 
         android:id="@+id/addMarker" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_gravity="center" 
         android:padding="16dp" 
         android:clickable="true" 
         android:src="@drawable/ic_add_circle_black_24dp" 
         android:background="@android:color/transparent" 
         android:tint="#C4252C"/> 

       </LinearLayout> 

       <LinearLayout 
        android:layout_weight="0.5" 
        android:layout_width="0dp" 
        android:layout_height="match_parent"> 

        <android.support.v7.widget.AppCompatImageButton 
         android:id="@+id/removeMarker" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:layout_gravity="center" 
         android:background="@android:color/transparent" 
         android:padding="16dp" 
         android:src="@drawable/ic_remove_circle_black_24dp" 
         android:tint="#C4252C"/> 

       </LinearLayout> 

      </LinearLayout> 


     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

對於全面性,繼承人,有關建立並顯示AlertDialog的代碼:

FragmentChordMenu.java

public class FragmentChordMenu extends Fragment implements CustomChordAdapter.onItemClickListener { 
public static int chordCanvasWidth, chordCanvasHeight; 

private static RecyclerView mCustomChordList; 
private static CustomChordAdapter mRecyclerViewAdapter; 
private static Context mContext; 

private FloatingActionButton mFAB; 
private View mPopupView; 
private AlertDialog mDialogChordMenu; 
private CustomChordCanvas chordCanvas; 
private ImageButton btnAddMarker, btnRemoveMarker; 

@Override 
public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    mRecyclerViewAdapter = new CustomChordAdapter(this); 
} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    mContext = getActivity(); //stores application context for later use in fragment without risk 
                 //of detachment 

    View v = inflater.inflate(R.layout.menu_fragment_chord, container, false); 
    LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    mPopupView = layoutInflater.inflate(R.layout.menu_popup_set_chords, null); 

    mDialogChordMenu = new AlertDialog.Builder(getActivity()).setView(mPopupView).create(); 

    chordCanvas = (CustomChordCanvas) mPopupView.findViewById(R.id.chordCanvas); 

    ... 

    //This method fetches view parameters for one of the Dialog child layout 
    //A ViewTreeObserver is used to allow view parameters to be accessed BEFORE view is drawn on screen 
    ViewTreeObserver viewTreeObserver = chordCanvas.getViewTreeObserver(); 
    if (viewTreeObserver.isAlive()) { 
     viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       chordCanvas.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       chordCanvasWidth = chordCanvas.getMeasuredWidth(); 
       chordCanvasHeight = chordCanvas.getMeasuredHeight(); 
       CustomChordCanvas.setViewParams(chordCanvasWidth, chordCanvasHeight); 
      } 
     }); 
    } 

    mFAB = (FloatingActionButton) v.findViewById(R.id.addChord); 
    mFAB.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      mDialogChordMenu.show(); 
      mCustomChordList = (RecyclerView) mPopupView.findViewById(R.id.rv_userChords); 
      LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); 
      mCustomChordList.setLayoutManager(layoutManager); 
      mCustomChordList.setAdapter(mRecyclerViewAdapter); 
     } 
    }); 

    return v; 
} 
...} 
+0

嘗試給定高度寬度以對話自定義佈局。 –

回答

1

將一些默認高度設置爲對話框的自定義佈局。

+0

對高度進行編碼確實會使缺失的線性佈局顯示!所以現在的問題是,如何讓我的原始佈局權重適用於兩種佈局(0.10和0.90)? – Cody