2017-01-03 28 views
0

我有一個xml,其根目錄是GridLayout,我希望它在AlertDialog內顯示。 這裏是我在我的片段onResume()內使用的代碼:如何在GridLayout中包裝AlertDialog?

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
LayoutInflater inflater = getActivity().getLayoutInflater(); 
final View addViewImg = inflater.inflate(R.layout.matrix_image, null); 
builder.setView(addViewImg); 
AlertDialog alertDialog = builder.create(); 
Window window = alertDialog.getWindow(); 
//I tried with "setLayout" but it doesn't work 
window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
alertDialog.show(); 

這裏是:matrix_image.xml

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/matrixLayout" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 

    <ImageButton 
     android:id="@+id/imageUpLeft" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <!-- Other 8 ImageButtons composing a 3x3 grid --/> 

</GridLayout> 

的問題是,View不在AlertDialog的正中央......也不是AlertDialog迴繞GridLayout

The asymmetrical AlertDialog I get

如何獲得右側擺脫白色空間和包裹AlertDialog周圍的網格佈局

注意:我嘗試了幾個在這裏提出的選項在stackoverflow,但他們都沒有爲我工作。也許GridLayout的行爲有點不同?

回答

1

我不確定它是否適合你,但是你可以使用支持庫中的GridLayout。然後,將具有佈局這樣的:

<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/matrixLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:columnCount="3" 
    app:rowCount="3"> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_columnWeight="1" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

</android.support.v7.widget.GridLayout> 

您將有結果這樣的:

enter image description here

+0

我得到了很多錯誤,對於大多數你使用的屬性,例如:'錯誤:(1 )沒有找到包'my.package.path.here'中的屬性'columnCount'的資源標識符。怎麼了?感謝您的答案btw! @Divers – Robb1

+0

您確定您已閱讀我的答案嗎? '你可以使用支持庫中的GridLayout。 '等等。您是否使用支持庫中的GridLayout? – Divers

+0

對不起,但因爲我是一個新手我不知道如何導入該庫...你能更具體嗎?感謝耐心! – Robb1