如何从android中的Alert对话框中删除多余的空间

时间:2017-02-12 11:38:58

标签: android

在我的Android应用程序中,它是一个警告对话框,但有一个问题它从标题顶部有额外的空间。如何删除额外的空间任何人都可以帮助我。

In this image there is an alert dialog but I want to remove extra spaces from above welcome title

java code

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_welcome_from);

    final AlertDialog.Builder builder =
            new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);

    Typeface tfmain = Typeface.createFromAsset(getAssets(), fontFooterpath);
    Typeface tfContent = Typeface.createFromAsset(getAssets(), fontMain);

    CustomTFSpan tfSpan = new CustomTFSpan(tfContent);
    SpannableString spannableString = new SpannableString("Welcome");
    spannableString.setSpan(tfSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setTitle(spannableString);

    CustomTFSpan tfSpan1 = new CustomTFSpan(tfmain);
    SpannableString spannableString1 = new SpannableString("Hello");
    spannableString1.setSpan(tfSpan1, 0, spannableString1.length(), spannableString1.SPAN_EXCLUSIVE_EXCLUSIVE);
    builder.setMessage(spannableString1);

    builder.setPositiveButton("START", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Intent ii = new Intent(WelcomeFromActivity.this, Question1Activity.class);
            startActivity(ii);
        }
    });
    builder.setCancelable(false); //for prevent outside touch
    builder.show();

}

任何人都知道如何从警报对话框标题中删除多余的空间。

2 个答案:

答案 0 :(得分:2)

为AlertDialog使用自定义样式。

将以下样式添加到style.xml。

<style name="MyCustomTheme" parent="Theme.AppCompat.Light.Dialog.Alert">>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>     
</style>

将主题设置为活动中的AlertDialog:

final AlertDialog.Builder builder =
            new AlertDialog.Builder(this, R.style.MyCustomTheme);

答案 1 :(得分:1)

有服务器方式:-

  1. 警报对话框

    AlertDialog.Builder builder;
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
             builder = new AlertDialog.Builder(activityContext, android.R.style.Theme_Material_Light_Dialog_NoActionBar); // To remove extra space
         } else {
             builder = new AlertDialog.Builder(activityContext);
         }
    
    builder.setTitle(dialogTitle);
    builder.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss(); // Close dialog
           }
        });
    builder.setCancelable(false); //to make Dialog's outside non-touchable
    builder.show(); // to show dialog
    
  2. 自定义对话框

    在style.xml文件中进行定制设计,您可以根据需要添加更多值。

    <style name="CustomDesign" parent="Theme.AppCompat.Light.Dialog.Alert">>
          <item name="android:layout_width">wrap_content</item>
          <item name="android:layout_height">wrap_content</item>
          <item name="android:windowBackground">@null</item>
          <item name="android:windowNoTitle">true</item>
    </style>
    

    将此样式设置为“警报对话框”或任何对话框。

    对于警报对话框:-

    AlertDialog.Builder builder =
        new AlertDialog.Builder(this, R.style.CustomDesign);
    

    对于对话框:-

    Dialog customDialog= new Dialog(this,R.style.CustomDesign);
    
  3. 对话框

    Dialog dialog = new Dialog(context);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); // To remove extra space
    dialog.setContentView(R.layout.layout); // to set custom layout
    dialog.show();