5

このダイアログクラスがあり、アイコンをそのタイトルに設定したい:

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」で、テキストだけで、アイコンは表示されません。なぜお願いしますか、解決策は何ですか、どうもありがとうございました

4

3 に答える 3

16

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

代わりに私のものを追加してください!

于 2013-02-17T20:11:59.763 に答える
3

これがカスタムテーマとビューのダイアログです

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

私の例があなたのためにそれをクリアすることを願っています、そしてあなたが望むものがあれば、他の人がそれを簡単にすることができるように答えを受け入れてください。ありがとう

于 2013-02-17T19:54:21.003 に答える
1

Dialog.setTitle(int resId)は、タイトルテキストを文字列リソースに設定する場合に使用されます。

あなたが探しているのは、あなたがすでに行っていることです-setContentView。カスタムxmlで、タイトルを好みのように見せます。または、実行時に設定する場合は、ImageViewへの参照を取得してコードで設定します。

お役に立てれば。

于 2013-02-17T19:47:08.657 に答える