2016-10-11 38 views
0

我想在應用程序中創建自己的對話框。當我在我的xml中使用layout_weight時,我在我的logcat中獲得了ViewRootImpl #2 Surface is not valid,但是當我使用固定的layout_widthlayout_height時,一切似乎都沒有問題。誰能告訴我,我哪裏錯了?Android自定義對話框佈局錯誤

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="4"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="horizontal" 
     android:weightSum="4" 
     android:background="@color/white"> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/imageButton_spades" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="30dp" 
      android:background="@color/orange"/> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/imageButton_hearts" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="30dp" 
      android:background="@color/orange" 
      /> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/imageButton_clubs" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="30dp" 
      android:background="@color/orange"/> 

     <ImageButton 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:id="@+id/imageButton_diamonds" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="30dp" 
      android:background="@color/orange"/> 

    </LinearLayout> 


     <LinearLayout 
      android:id="@+id/layout_figures" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="3" 
      android:orientation="horizontal" 
      android:weightSum="4" 
      android:background="@color/material_blue_grey_800"> 

      <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:id="@+id/imageButton_ace" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="30dp" 
       android:background="@color/orange"/> 

      <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:id="@+id/imageButton_king" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="30dp" 
       android:background="@color/orange"/> 

      <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:id="@+id/imageButton_queen" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="30dp" 
       android:background="@color/orange"/> 

      <ImageButton 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:id="@+id/imageButton_jack" 
       android:layout_marginLeft="10dp" 
       android:layout_marginRight="10dp" 
       android:layout_marginTop="30dp" 
       android:background="@color/orange"/> 

     </LinearLayout> 


</LinearLayout> 

回答

1

試試這個沒有給予任何的高度寬度:

 Dialog dialog = new Dialog(context); 
     dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialog.setContentView(R.layout.dialog_friend_data); 

     /*******************dialog features*********************/ 
     final Window window = dialog.getWindow(); 
     window.setBackgroundDrawable(new ColorDrawable(Color.WHITE)); 
     window.setLayout(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); 
     window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

     window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 

     dialog.show(); 
+0

你的意思是我應該從所有XML元素刪除高度和寬度? –

+0

我不知道你在佈局中做了什麼。如果您的對話框拉伸且對話框顯示效果不佳,則可以使用包含對話框的窗口。它會自動適應每個屏幕。 –

+0

如果我在Android Studio中查看預覽,則佈局看起來不錯。但是,當我嘗試在應用程序中加載它時,高度非常小,並且不會顯示「ImageButtons」。我試過你的解決方案,但它沒有工作 –