如何从Android中的对话框中删除标题部分?

时间:2017-11-29 05:39:38

标签: android

我想删除Android中对话框中的标题部分,我想将自己创建的xml文件用作对话框。

3 个答案:

答案 0 :(得分:0)

以下是相同的代码:

    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext());
            LayoutInflater layoutInflater = getActivity().getLayoutInflater();
            final ViewGroup viewGroup = null;
            View dialogView = layoutInflater.inflate(R.layout.custom_email_dialog, viewGroup, false);

            dialogBuilder.setView(dialogView);
            final AlertDialog alertDialog = dialogBuilder.create();
            alertDialog.show();

这里的“custom_email_dialog”是我们需要在对话框中显示的自定义XML。

您可以访问对话框元素:

  TextView headerTextView = (TextView) dialogView.findViewById(R.id.emailHeader);

答案 1 :(得分:0)

您可以为自己的布局充气并将其设置为对话框。

Dialog dialog = new Dialog(ProfileSettingsActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.uploadphoto); // Your layout here
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT; //Custom width and height
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);
dialog.setCancelable(true);
dialog.show();

答案 2 :(得分:0)

只需在对话框中添加样式并删除自定义对话框的标题即可。

<强> Custom_dialog.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="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txt_warning"`enter code here`
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:text="Warning"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textSize="18dp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="Are you sure you want to Logout?"
        android:textColor="@android:color/white"
        android:textSize="18dp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="15dp"
        android:background="@android:color/white" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        android:weightSum="1">

        <Button
            android:id="@+id/btn_no"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight=".5"
            android:background="@color/colorPrimary"
            android:clickable="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="No"
            android:textColor="@android:color/white"
            android:textSize="18dp" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/white" />

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:background="@color/colorPrimary"
            android:clickable="true"
            android:gravity="center"
            android:padding="10dp"
            android:text="Yes"
            android:textColor="@android:color/white"
            android:textSize="18dp" />

    </LinearLayout>

</LinearLayout>

在Styles.xml中设置样式: -

<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
    </style>

在按钮上单击侦听器,您必须编写此代码: -

//自定义对话框

 final Dialog dialog = new Dialog(mContext, R.style.FullHeightDialog);
 dialog.setContentView(R.layout.custom_dialog);
 dialog.setCancelable(false);

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(lp);

dialog.show();

Button btnYes = (Button) dialog.findViewById(R.id.btn_yes);
Button btnNo = (Button) dialog.findViewById(R.id.btn_no);
 // if button is clicked, close the custom dialog
 btnYes.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            documentImage = documentImagesList.get(getAdapterPosition());

                          //logout code here
                        }
});
btnNo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dialog.dismiss();
                        }
});