簡體   English   中英

android對話框設置圖標到標題

[英]android dialog set icon to title

我有這個對話框類,我想將圖標設置為其標題:

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”只是文字,沒有出現圖標,為什么請,解決方案是什么,非常感謝

button任何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();
}

並添加我的!

這是一個包含自定義主題和視圖的對話框

        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");

希望我的例子為你清楚,如果你想要的話請接受答案,以便其他人可以輕松搞定。 謝謝

如果要將標題文本設置為字符串資源,則使用Dialog.setTitle(int resId)。

您正在尋找的是您正在做的事情 - setContentView。 在您的自定義xml中,標題看起來與您喜歡的一樣,或者 - 如果您希望在運行時設置它,只需獲取對ImageView的引用並在代碼中設置它。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM