2010-09-09 128 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调用它是正确的地方:)。