2017-07-03 60 views
0

我製作了一個AlertDialog,它有三個按鈕(正面,負面,中性),當我在手機上測試時它工作正常(Android 7.0),但我有幾個朋友與Android 5.0和6.0)測試它們,他們只有中性按鈕顯示。AlertDialog僅在Android上顯示中性按鈕<7.0

下面是代碼:

AlertDialog.Builder isSelfieBuilder = new AlertDialog.Builder(context); 
isSelfieBuilder.setTitle("Choose picture orientation."); 

// Set up the buttons 
isSelfieBuilder.setPositiveButton("Selfie", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     //Do something 
    } 
}); 
isSelfieBuilder.setNeutralButton("Landscape", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     //Do something 
    } 
}); 
isSelfieBuilder.setNegativeButton("Portrait", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     //Do something 
    } 
}); 
final AlertDialog picTypeDialog = isSelfieBuilder.create(); 
picTypeDialog.show(); 

final Button positiveButton = picTypeDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
LinearLayout.LayoutParams positiveButtonLL = (LinearLayout.LayoutParams) positiveButton.getLayoutParams(); 
positiveButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT; 
positiveButton.setLayoutParams(positiveButtonLL); 

final Button neutralButton = picTypeDialog.getButton(AlertDialog.BUTTON_NEUTRAL); 
LinearLayout.LayoutParams neutralButtonLL = (LinearLayout.LayoutParams) neutralButton.getLayoutParams(); 
neutralButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT; 
neutralButton.setLayoutParams(neutralButtonLL); 

final Button negativeButton = picTypeDialog.getButton(AlertDialog.BUTTON_NEGATIVE); 
LinearLayout.LayoutParams negativeButtonLL = (LinearLayout.LayoutParams) negativeButton.getLayoutParams(); 
negativeButtonLL.width = ViewGroup.LayoutParams.MATCH_PARENT; 
negativeButton.setLayoutParams(negativeButtonLL); 

任何人有任何想法,爲什麼我只在那裏得到的中性按鈕?我嘗試刪除對齊部分(.show()後的所有內容),但我得到了相同的結果。我正在使用API​​ 21(5.0)的模擬器上測試,並且遇到同樣的問題。

+0

確定它必須使用android版本做些什麼?也許更多的屏幕尺寸導致它.... – Opiatefuchs

回答

1
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setTitle("Choose picture orientation.") 
       .setPositiveButton("Selfie", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //doSomething 
        } 
       }) 
       .setNeutralButton("Landscape", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //doSomething 
        } 
       }) 
       .setNegativeButton("Portrait", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //doSomething 
        } 
       }); 
     AlertDialog dialog = builder.create(); 
     dialog.show(); 
0

所以我的「固定」的方式,這是通過完全禁用:

AlertDialog picTypeDialog = isSelfieBuilder.create(); 
picTypeDialog.show(); 

而且隨着

isSelfieBuilder.show(); 

現在適用於所有設備優良,即使間距簡單地更換它之間的按鈕是不完美的,我不能編輯按鈕佈局沒有AlertDialog