2016-09-29 53 views
0

我被卡住了marginpadding問題的標題AlertDialog.Builder,我正在做的是我想要的標題在center所以我使用setCustomTitle我能夠這樣做,但堅持與保證金和填充它。我不想要不必要的填充顯示,也想設置一些頂部邊緣標題,我使用LinearLayout.LayoutParams,但它沒有效果。請建議怎樣做才能處理it.thanks如何設置AlertDialog自定義標題頂部邊距並刪除不需要的填充?

代碼:

dialog = new AlertDialog.Builder(context, R.style.DialogTheme); 
    TextView title = new TextView(context); 
    title.setTextColor(ContextCompat.getColor(context, R.color.black)); 
    title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); 
    title.setTypeface(Typeface.DEFAULT_BOLD); 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    lp.setMargins(0, 20, 0, 0); 
    title.setLayoutParams(lp); 
    title.setText("Dialog"); 
    title.setGravity(Gravity.CENTER); 
    dialog.setCustomTitle(title); 
    dialog.setMessage("Dialog box with custom title view "); 
    dialog.setCancelable(false); 
    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 

     } 
    }); 
    dialog.show(); 

結果:enter image description here

+0

看起來不錯!我在padding中看不到任何問題。如果你想在'dialog''setContentView()'中使用重力概念,可以將文本**置中**! –

+0

@utkarshdubey我不想在對話框標題和對話框消息之間有很多填充,這是默認結果 –

回答

1
AlertDialog.Builder dialog = new AlertDialog.Builder(this); 
     TextView title = new TextView(this); 
     title.setTextColor(ContextCompat.getColor(this, android.R.color.black)); 
     title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); 
     title.setTypeface(Typeface.DEFAULT_BOLD); 
     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     lp.setMargins(0, 20, 0, 0); 
     title.setPadding(0,30,0,0); 
     title.setLayoutParams(lp); 
     title.setText("Dialog"); 
     title.setGravity(Gravity.CENTER); 
     dialog.setCustomTitle(title); 
     dialog.setMessage("Dialog box with custom title view "); 
     dialog.setCancelable(false); 
     dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 

      } 
     }); 
     dialog.show(); 

替換此代碼,將工作

+0

感謝'setPadding'工作,但如何刪除對話框的標題和消息之間的填充任何想法? –

+0

@KapilRajput嘗試設置自定義對話框而不是警報對話框它將提供更多的自定義 –

0

你能避免設置這一切方法通過使用DialogFragment到您的AlertDialog。只需在onCreateView方法中使用getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);方法,並以創建公共片段的方式爲對話框創建自己的自定義視圖。這是進行適當對話的更好方法。

0

我建議你使用DialogFragment顯示自定義佈局警告對話框其很容易,我們可以自定義我們的對話框按requirement..for更詳細的瞭解吧:https://developer.android.com/reference/android/app/DialogFragment.html

下面是佈局按照您的要求,您可以使用下面的xml佈局來顯示您的自定義佈局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="10dp"> 

    <LinearLayout 
     android:id="@+id/header" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="@dimen/margin10dp" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="My Dialog" 
      android:textColor="@color/white" 
      android:textSize="22dp" 
      android:textStyle="bold" /> 

    </LinearLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/header" 
     android:padding="10dp"> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="My Dialog Box Description !" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textSize="20dp" /> 

     <Button 
      android:id="@+id/btnSubmit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:layout_below="@+id/textView2" 
      android:background="@android:color/transparent" 
      android:text="OK" 
      android:textSize="22dp" /> 

    </RelativeLayout> 
</RelativeLayout> 
相關問題