简体   繁体   中英

Remove Android alert dialog empty space at the top

I have an Android layout that's used for showing an "about app" alert dialog with a "Close" button. It has a title in bold centered, the app version and the developer name. Everything looks fine except that there's a very noticeable margin/padding above the title and I can't remove that empty space.

Here's the layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:orientation="vertical" android:scrollbars="none" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">
  <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginRight="6.0dp" xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_gravity="center">
      <TextView android:textSize="16.0dip" android:textStyle="bold" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginRight="6.0dip" android:layout_marginBottom="10.0dip" android:text="@string/app_name" />
    </LinearLayout>

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal">
        <TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginTop="0.0dip" android:text="@string/version_text" />
        <TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:layout_marginTop="0.0dip" android:text="@string/app_version" />
    </LinearLayout>

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical">
      <TextView android:textSize="14.0dip" android:textColor="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="6.0dip" android:text="@string/about_developer" />
    </LinearLayout>

    <LinearLayout android:gravity="center" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10.0dip" xmlns:android="http://schemas.android.com/apk/res/android">
      <Button android:id="@id/close_btn" android:layout_width="110.0dip" android:layout_height="44.0dip" android:layout_marginTop="4.0dip" android:layout_marginBottom="4.0dip" android:text="@string/about_close" />
    </LinearLayout>

  </LinearLayout>
</ScrollView>

And here's what it shows:

关于对话窗口

I have tried adding android:layout_marginTop="-10.0dip" attribute in many tags and searched SO for a similar issue but I can't find it. I'd be thankful if someone shows me how solve this problem.

You could try to make your own Dialog with a custom View by using a DialogFragment and set this

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

Or maybe just try to use a simple Dialog , not an AlertDialog like this

Dialog dialog = new Dialog(theContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(yourCustomView);
dialog.show();
  1. Create style for the AlertDialog:
    <style name="AlertDialogCustom" parent="@android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
        <item name="android:background">#ffffff</item>
        <item name="android:textColorPrimary">#000000</item>
        <item name="android:textColor">@000000</item>
        <item name="android:typeface">monospace</item>
        <item name="android:textSize">12sp</item>
    </style>
  1. Define this style while creating AlertDialog

    AlertDialog.Builder builder = new AlertDialog.Builder(container.getContext(), R.style.AlertDialogCustom);

Use a RelativeLayout instead (the code below needs to be adjusted to fit your needs, but it gives the general layout):

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

<TextView
    android:text="App Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/textView" />

<TextView
    android:text="Version 1.0"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="29dp"
    android:id="@+id/textView2" />

<TextView
    android:text="Developed by Name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="23dp"
    android:id="@+id/textView3" />

<Button
    android:text="Close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView3"
    android:layout_alignLeft="@+id/textView2"
    android:layout_alignStart="@+id/textView2"
    android:layout_marginTop="42dp"
    android:id="@+id/button2" />
</RelativeLayout>

You can also use a Dialog instead like others have mentioned.

EDIT:

You can try using a custom Dialog instead then using a DialogFragment. To call the Dialog, use this: new CustomDialogFragment().show(getSupportFragmentManager(), "mydialog");

Then create a new class extending DialogFragment, and use something similar to this:

public class DonateDialogFragment extends DialogFragment implements View.OnClickListener
{

private AlertDialog dialog;

public interface CustomDialogListener {
    void buttonClicked();
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    if (!(activity instanceof CustomDialogListener)) {
        throw new ClassCastException(activity.toString() + " must implement CustomDialogListener");
    }
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder customDialogMain = new AlertDialog.Builder(getActivity());
    donateDialogMain.setCancelable(true);
    LayoutInflater inflater = getActivity().getLayoutInflater();

    View view = inflater.inflate(R.layout.--YOUR-CUSTOM-DIALOG-LAYOUT-HERE--, null);

    view.findViewById(R.id.close_btn).setOnClickListener(this);

    customDialogMain.setView(view);

    dialog = customDialogMain.show();
    return dialog;
}

public void onClick(View v) {
    // does something very interesting
    switch (v.getId())
    {
        case R.id.close_btn:
            ((CustomDialogListener) getActivity()).buttonClicked();
            dialog.dismiss();
            break;
    }
}

}

you can use this style direct to style.xml

<style name="Theme.UserDialog.AppCompat" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowCloseOnTouchOutside">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:minWidth">600dp</item>
</style>

Apply this style to the activity class which you wants to set as alert dialog

android:theme="@style/Theme.UserDialog.AppCompat"

and start activity using Intent like

Intent activity= new Intent(this, AlertActivity.class);
startActivity(activity);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM