How to remove title from custom dialog?

有些话、适合烂在心里 提交于 2019-12-13 07:57:12

问题


I implemented a layout in order to be my custom dialog layout like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@color/gray_back"
   xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
    android:id="@+id/mainLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp"
    android:layout_centerInParent="true">

    <ImageView
        android:src="@drawable/user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="center"
        android:layout_gravity="center"
        android:contentDescription="@string/app_name" />

    <EditText
        android:id="@+id/username"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center"
        android:layout_gravity="center"
        android:hint="@string/new_profile_name" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/dividerHorizontal" />

</LinearLayout>

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_below="@id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="0dip"
    android:measureWithLargestChild="true">

    <Button
        android:id="@+id/cancel"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel"/>
    <Button
        android:id="@+id/ok"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Ok"/>
</LinearLayout>

And I created it programmatically like this:

final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_add_profile);

Button dialogButton = (Button) dialog.findViewById(R.id.cancel);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         dialog.dismiss();
       }
   });
 dialog.show();

But then my dialog is showing like this:

How can I delete the remaining space from the image to the top of the layout?

I wanted to try this:

dialog.setWindowFeature(Window.WINDOW_NO_TITLE);

but it crashes... How can I remove that remaining space?


回答1:


Create a new style, add:

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

And finally :

Dialog d = new Dialog(this, R.style.nameOfStyle);

Edit: This answer




回答2:


try using DialogFragment class, it doesn't force title on custom views. you can make it a static nested class in your activity.

public static class MyDialogFragment extends DialogFragment {
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(R.layout.custom_view);
    return builder.create();
  }
}

make your Activity extend AppCompatActivity, use imports

import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;

then to show the dialog instantiate the dialog fragment call show() passing support fragment manager and a string tag as follows :

MyDialogFragment dialogFragment = new MyDialogFragment();
    dialogFragment.show(getSupportFragmentManager(), "f1");


来源:https://stackoverflow.com/questions/34041237/how-to-remove-title-from-custom-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!