gpt4 book ai didi

android - AlertDialog 按钮的图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:14 26 4
gpt4 key购买 nike

是否可以将可绘制对象添加到 AlertDialog 的positivenegativeneutral 按钮?如果是,那么如何?

最佳答案

由于 onPrepareDialog 已弃用,您可以改用 onShowListener

您还应该设置 Drawable 边界,否则它将被放置在最左边。

下面代码的输出

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;
}
}

关于android - AlertDialog 按钮的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3679432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com