2010-09-09 66 views

回答

13

由於onPrepareDialog已被棄用,你可以使用onShowListener代替。

此外,您應該設置可繪製邊界或將它放置在最左邊。代碼的下面

Output of Code below

public class MyDialog extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     final AlertDialog dialog = new AlertDialog.Builder(getActivity()) 
       .setTitle("My Dialog") 
       .setNegativeButton("Cancel", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
        } 
       }).setPositiveButton("Play", new OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
        } 
       }).create(); 
     dialog.setOnShowListener(new OnShowListener() { 

      @Override 
      public void onShow(DialogInterface dialogInterface) { 
       Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 

       // if you do the following it will be left aligned, doesn't look 
       // correct 
       // button.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_media_play, 
       // 0, 0, 0); 

       Drawable drawable = getActivity().getResources().getDrawable(
         android.R.drawable.ic_media_play); 

       // set the bounds to place the drawable a bit right 
       drawable.setBounds((int) (drawable.getIntrinsicWidth() * 0.5), 
         0, (int) (drawable.getIntrinsicWidth() * 1.5), 
         drawable.getIntrinsicHeight()); 
       button.setCompoundDrawables(drawable, null, null, null); 

       // could modify the placement more here if desired 
       // button.setCompoundDrawablePadding(); 
      } 
     }); 
     return dialog; 
    } 
} 
+0

謝謝你的更新:) – 2013-12-30 07:18:04

+0

你好,我想顯示圖標更接近播放文本。所以你可以請我建議嗎? – 2014-09-12 05:54:37

+0

你可以舉一個如何使用你的代碼的例子嗎?如何實例化對話框? – 2014-11-29 21:28:34

7

後你已經建立了AlertDialogonCreateDialog您可以在onPrepareDialog使用下面的代碼將圖像添加到正按鈕:

@Override 
protected void onPrepareDialog(int id, Dialog dialog) { 
    super.onPrepareDialog(id, dialog); 
    AlertDialog alertDialog = (AlertDialog)dialog; 
    Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); 
    button.setCompoundDrawablesWithIntrinsicBounds(this.getResources().getDrawable(
      R.drawable.icon), null, null, null); 

} 

試圖將繪製添加到按鈕在onCreateDialog方法不似乎工作。

+0

會試着回到這個弗蘭克。 – 2010-09-17 07:15:29

4

你不能在onCreateDialog添加按鈕,必須做它在onPrepareDialog因爲AlertDialog在一個非常特殊的方式,通過機器人處理:

你其實並不真正持有到真正的對話時參考你使用alert對話框,通過使用AlertDialog.Builder.create()獲得的對象只是內部控制器的一個面。

而在創建之前實際上是調用的,在jvm中沒有這樣的控制器。只是門面。所以,直到這個方法被調用(在onCreateDialog結束時,如果你讓你的活動管理自己的對話框),真正的控制器不存在,真正的按鈕不會。以後打電話

alert.show(); 
Button email = alert.getButton(AlertDialog.BUTTON_NEUTRAL); 
email.setBackgroundResource(R.drawable.email); 

請注意,您必須使用getButton():

全新的SOF評議,斯特凡

5

這可以通過獲取使用getButton()方法的按鈕的引用來完成show()方法,否則你會得到一個NullPointerException ..

+0

將檢查並回復給您。謝謝。 – 2012-03-31 12:36:42

0

1.first創建一個新的佈局文件來存儲imagebuttons:new_layout.xml;

<?xml version="1.0" encoding="UTF-8" ?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_margin="15dp" 
    android:gravity="center_horizontal" 
    android:background = "#FFFFFF" 
    android:orientation="horizontal"> 

    <!-- game button --> 
    <ImageButton 
     android:id="@+id/game" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_gravity="bottom" 
     android:background = "#00ffffff" 
     android:src="@drawable/game"/> 

    <!-- browser button --> 
    <ImageButton 
     android:id="@+id/browser" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_gravity="bottom" 
     android:background = "#00ffffff" 
     android:src="@drawable/browser"/> 

    <!-- email button --> 
    <ImageButton 
     android:id="@+id/email" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_margin="5dp" 
     android:layout_gravity="bottom" 
     android:background = "#00ffffff" 
     android:src="@drawable/email"/> 

</LinearLayout> 

2.加下面的代碼到你想要的對話顯示:

final AlertDialog alertDialog = new AlertDialog.Builder(TalkerActivity.this).create(); 
    alertDialog.show(); 
    Window win = alertDialog.getWindow(); 
    win.setContentView(R.layout.new_layout); 

    //Game 
    ImageButton game_btn = (ImageButton)win.findViewById(R.id.game); 
    game_btn.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    //Browser 
    ImageButton browser_btn = (ImageButton)win.findViewById(R.id.browser); 
    browser_btn.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    //Email 
    ImageButton email_btn = (ImageButton)win.findViewById(R.id.email); 
    email_btn.setOnClickListener(new OnClickListener(){ 
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

鏈接:http://blog.csdn.net/willproud/article/details/9191971

2

輸出作爲@aaronvargas說,使用onShowListener。我會稍微改進他的回答,因爲對於較老/較小的設備,圖像與文本重疊。下面是onShow代碼:

@Override 
public void onShow(DialogInterface dialogInterface) { 
    Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 
    button.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_img, 0, 0, 0); 
    Utils.centerImageAndTextInButton(button); 
} 

下面是一個效用函數居中Button內部的左圖像和文本:

public static void centerImageAndTextInButton(Button button) { 
    Rect textBounds = new Rect(); 
    //Get text bounds 
    CharSequence text = button.getText(); 
    if (text != null && text.length() > 0) { 
     TextPaint textPaint = button.getPaint(); 
     textPaint.getTextBounds(text.toString(), 0, text.length(), textBounds); 
    } 
    //Set left drawable bounds 
    Drawable leftDrawable = button.getCompoundDrawables()[0]; 
    if (leftDrawable != null) { 
     Rect leftBounds = leftDrawable.copyBounds(); 
     int width = button.getWidth() - (button.getPaddingLeft() + button.getPaddingRight()); 
     int leftOffset = (width - (textBounds.width() + leftBounds.width()) - button.getCompoundDrawablePadding())/2 - button.getCompoundDrawablePadding(); 
     leftBounds.offset(leftOffset, 0); 
     leftDrawable.setBounds(leftBounds); 
    } 
} 

這最後一個函數使用Button的寬度進行計算,所以你必須檢查你是否在正確的地方打這個電話。也就是說,寬度應該不是零。在這種情況下,從onShow調用它是正確的地方:)。