如何向对话框添加布局

时间:2012-08-27 13:48:41

标签: android android-layout android-widget android-dialog

我有一个想法,但我不确定如何实现它

首先我有一个对话框

final Dialog dialog = new Dialog(mContext);

我也有布局

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

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textViewDescription"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textViewWhen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

我想要的是在对话框中添加此布局,我可能还想在该对话框中添加更多相同的布局 我怎么能这样做?

例如,如何在一个对话框中添加两个此布局?

之类的东西
  Dialog Title

   Large Text
   Small Text
   Medium Text


   Large Text
   Small Text
   Medium Text

5 个答案:

答案 0 :(得分:6)

这样的事情:

LayoutInflater li = LayoutInflater.from(SomeActivity.this);
someLayout = (LinearLayout)li.inflate(R.layout.some_layout, null);

alert = new AlertDialog.Builder(SettingsActivity.this);
alert.setView(someLayout);

答案 1 :(得分:3)

这是我的申请中的一个例子:

public class ConfirmDialog extends DialogFragment {

public static String TAG = "Confirm Dialog";

public interface ConfirmDialogCompliant {
    public void doOkConfirmClick();
    public void doCancelConfirmClick();
}

private ConfirmDialogCompliant caller;
private String message;

public ConfirmDialog(ConfirmDialogCompliant caller, String message){
    super();
    this.caller = caller;
    this.message = message;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.confirm_dialog, container, false);
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
    ((TextView) view.findViewById(R.id.textview_confirm)).setText(message);
    ((Button) view.findViewById(R.id.ok_confirm_button)).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // When button is clicked, call up to owning activity.
            caller.doOkConfirmClick();
        }
    });
    ((Button) view.findViewById(R.id.cancel_confirm_button)).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            // When button is clicked, call up to owning activity.
            caller.doCancelConfirmClick();
        }
    });
    return view;
}

}

其中膨胀的布局为confirm_dialog.xml 您可以在onCreateView方法中扩展您的布局 在这种情况下,我使用了DialogFragment(我建议您使用它...请参阅支持库,这样您就不必担心目标SDK了),但同样适用于Dialog。
希望它能帮到你!

答案 2 :(得分:2)

您可以查看此文档页面,其中说明如何在对话框

上添加自定义布局

http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

关键是setContentView方法:

dialog.setContentView(R.layout.custom_dialog);

答案 3 :(得分:2)

看看这个:

how to get customized alert dialog , like the one shown in image?

参考我给出的答案(Aamir Shah)

答案 4 :(得分:1)

使用DialogFragment,与其他任何Fragment一样,您可以完全自定义布局。它可以在v4支持库中找到。

http://developer.android.com/reference/android/app/DialogFragment.html

http://developer.android.com/tools/extras/support-library.html