代码之家  ›  专栏  ›  技术社区  ›  Ragunath Jawahar cephus

AlertDialog按钮的图像

  •  9
  • Ragunath Jawahar cephus  · 技术社区  · 14 年前

    有没有可能在桌子上加些抽屉 积极的 , 消极的 警报对话框的按钮?如果是,那怎么办?

    6 回复  |  直到 14 年前
        1
  •  14
  •   Rudolf Real    10 年前

    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;
        }
    }
    
        2
  •  7
  •   Ragunath Jawahar cephus    11 年前

    在你建造了 AlertDialog onCreateDialog 您可以在中使用以下代码 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);
    
    }
    

    OnCreate对话框 这种方法似乎行不通。

        3
  •  6
  •   shaylh    13 年前

    这可以通过使用getButton()方法获取对按钮的引用来实现:

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

        4
  •  5
  •   Snicolas    14 年前

    不能在onCreateDialog中添加按钮,必须在onPrepareDialog中添加按钮,因为android以非常特殊的方式处理AlertDialog:

    实际上,当您使用alert dialog时,您并没有真正持有对真实对话框的引用,alert dialog是您使用alert dialog获得的对象AlertDialog.Builder.create创建()只是内部控制器的一张脸。

    在实际调用create之前,jvm中没有这样的控制器。只是外表而已。因此,在调用此方法之前(如果让活动管理自己的对话框,则在onCreateDialog的末尾),真正的控制器不存在,真正的按钮也不存在。

    全新软件评论员,Stphane

        5
  •  2
  •   IvanRF    11 年前

    正如@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);
        }
    }
    

    最后一个函数使用 按钮 做计算,所以你 必须 onShow公司 正确的地方:)。

        6
  •  1
  •   willyang    12 年前

    1.首先创建一个新的布局文件来存储imagebuttonsnew_布局.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