在AlertDialog中添加自定义布局

时间:2018-11-30 02:48:06

标签: android alertdialog

如何使用xml作为对话框的布局?此类用于显示对话框,但问题是我想设置自己的布局。

 public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
    final Dialog dialog = new Dialog(mContext);
    dialog.setTitle("Rate " + APP_TITLE);

    LinearLayout ll = new LinearLayout(mContext);
    ll.setOrientation(LinearLayout.VERTICAL);

    TextView tv = new TextView(mContext);
    tv.setText("If you enjoy using " + APP_TITLE + ", please take a moment to rate it. Thanks for your support!");
    tv.setWidth(240);
    tv.setPadding(4, 0, 4, 10);
    ll.addView(tv);

    Button b1 = new Button(mContext);
    b1.setText("Rate " + APP_TITLE);
    b1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
            dialog.dismiss();
        }
    });
    ll.addView(b1);

    Button b2 = new Button(mContext);
    b2.setText("Remind me later");
    b2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    ll.addView(b2);

    Button b3 = new Button(mContext);
    b3.setText("No, thanks");
    b3.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (editor != null) {
                editor.putBoolean("dontshowagain", true);
                editor.commit();
            }
            dialog.dismiss();
        }
    });
    ll.addView(b3);

    dialog.setContentView(ll);
    dialog.show();
}

我希望它具有3个按钮和一张图片。有可能这样做吗?

4 个答案:

答案 0 :(得分:6)

您可以为对话框设置自定义布局,如下所示:

创建自定义布局文件:

custom.xml

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

<TextView
    android:id="@+id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFF" />

<TextView
    android:id="@+id/text2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#FFF"/>

</RelativeLayout>

然后在您的活动中:

// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title");

// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text1);
text.setText("Text view 1");

TextView text = (TextView) dialog.findViewById(R.id.text2);
text.setText("Text view 2");
dialog.show();

答案 1 :(得分:0)

  

DialogFragment现在是显示覆盖的规范方法;直接使用Dialog是considered bad practice

用法

  

自定义视图

<!-- fragment_edit_name.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edit_name"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="center" android:orientation="vertical"  >

    <TextView
        android:id="@+id/lbl_your_name" android:text="Your name" 
        android:layout_width="wrap_content" android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/txt_your_name"
        android:layout_width="match_parent"  android:layout_height="wrap_content" 
        android:inputType="text"
        android:imeOptions="actionDone" />
</LinearLayout>

和DialogFragment将是

import android.support.v4.app.DialogFragment;
// ...

public class EditNameDialogFragment extends DialogFragment {

    private EditText mEditText;

    public EditNameDialogFragment() {
        // Empty constructor is required for DialogFragment
                // Make sure not to add arguments to the constructor
                // Use `newInstance` instead as shown below
    }

    public static EditNameDialogFragment newInstance(String title) {
        EditNameDialogFragment frag = new EditNameDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_edit_name, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // Get field from view
        mEditText = (EditText) view.findViewById(R.id.txt_your_name);
        // Fetch arguments from bundle and set title
        String title = getArguments().getString("title", "Enter Name");
        getDialog().setTitle(title);
        // Show soft keyboard automatically and request focus to field
        mEditText.requestFocus();
        getDialog().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }
}

并在“活动”中显示对话框:

public class DialogDemoActivity extends AppCompatActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      showEditDialog();
  }

  private void showEditDialog() {
      FragmentManager fm = getSupportFragmentManager();
      EditNameDialogFragment editNameDialogFragment = EditNameDialogFragment.newInstance("Some Title");
      editNameDialogFragment.show(fm, "fragment_edit_name");
  }
}

答案 2 :(得分:0)

自定义提醒对话框

* using create custom alert dialog box*/

private void Multiple_spinner_alert(int position) {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.multiple_spinner_recycler_layout);
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.gravity = Gravity.CENTER;
    dialog.getWindow().setAttributes(lp);
    Window window = dialog.getWindow();
    final RecyclerView recycler_spinner = (RecyclerView) dialog.findViewById(R.id.recycler_spinner);
    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
    recycler_spinner.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true));
    recycler_spinner.setLayoutManager(layoutManager);
    mAdapter = new MultipleSpinnerRecyclerAdapter(context,  getSetList,listPK_ProviderTypeOptions_ID);
    recycler_spinner.setAdapter(mAdapter);

    final  TextView alert_tv_ok=(TextView)dialog.findViewById(R.id.alert_tv_ok);
    onChangeSelectedReceivers();

    alert_tv_ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { {

             // do your code 

            dialog.dismiss();
        }
    });

    dialog.show();
}
  • 我正在使用这样的自定义对话框膨胀布局
  • 根据您的要求设置对话框的高度和宽度

它可以帮助您

答案 3 :(得分:0)

在布局资源文件中,确保android:layout_width为

android:layout_height = "wrap_content"

不是

android:layout_height = "match_parent"

相关问题