android对话框设置图标到标题

时间:2013-02-17 19:40:21

标签: android android-dialog

我有这个对话框类,我想将图标设置为其标题:

public class DialogMealInformation extends Dialog implements
        android.view.View.OnClickListener {
    Context context;
    private TextView tv_information;
    private Button b_ok;
    private String information;

    public DialogMealInformation(Context context, String information) {
        // TODO Auto-generated constructor stub
        super(context);
        this.context = context;
        this.information = information;
        setContentView(R.layout.dialog_meal_information);
        getWindow().setLayout(android.view.ViewGroup.LayoutParams.FILL_PARENT,
                android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
        setTitle("Info !");

        initialize();
    }

尝试过这样:

setTitle(R.layout.dialog_simple_header);

dialog_simple_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_dialog_simple_header_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tv_information"
        android:drawableLeft="@drawable/more_information" />

</LinearLayout>

但之后的标题是“res / layout / dialog_simple_header.x”只是文字,没有图标出现,为什么请,解决方案是什么,非常感谢

3 个答案:

答案 0 :(得分:14)

button点击或onActivity或您希望此对话框显示的任何位置点按此操作。在//

之后每次都阅读我的评论
AlertDialog alertDialog = new AlertDialog.Builder(
                    this).create();
            alertDialog.setTitle("TITLE"); // your dialog title 
            alertDialog.setMessage("Your message"); // a message above the buttons
            alertDialog.setIcon(R.drawable.ic_home); // the icon besides the title you have to change it to the icon/image you have. 
            alertDialog.setButton("Got IT", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) { // here you can add a method  to the button clicked. you can create another button just by copying alertDialog.setButton("okay")
                }

            });
alertDialog.show();

帮我一个忙,删除这个

    public DialogMealInformation(Context context, String information) {
    // TODO Auto-generated constructor stub
    super(context);
    this.context = context;
    this.information = information;
    setContentView(R.layout.dialog_meal_information);
    getWindow().setLayout(android.view.ViewGroup.LayoutParams.FILL_PARENT,
            android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    setTitle("Info !");

    initialize();
}

然后添加我的!

答案 1 :(得分:2)

这是一个包含自定义主题和视图

的对话框
        final Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
        dialog.setContentView(R.layout.dialog_layout_third);
        ImageView image = (ImageView) dialog.findViewById(R.id.image2);
        //image.setImageResource(R.drawable.ic_launcher);
        image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                dialog.dismiss();
            }
        });

        dialog.show();
        dialog.setCancelable(false);

这里我将主题设置为透明,然后我使用dialog.SetContentView添加布局。在我的布局中,我只使用imageview,这是我对话框的布局。

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:id="@+id/onetimeedit"
    android:visibility="visible"
    >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/image2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"

            android:src="@drawable/drag" />

    </RelativeLayout>

</FrameLayout>

然后您可以添加textView作为标题,只需将其添加到布局中即可。和/或使用dialog.setTitle("MyTitleShouldByHere");

希望我的例子为你清楚,如果你想要的话请接受答案,以便其他人可以轻松搞定。感谢

答案 2 :(得分:1)

如果您希望将标题文本设置为字符串资源,则使用Dialog.setTitle(int resId)。

您正在寻找的是您正在做的事情 - setContentView。在您的自定义xml中,标题看起来与您喜欢的一样,或者 - 如果您希望在运行时设置它,只需获取对ImageView的引用并将其设置为代码。

希望这有帮助。

相关问题