2016-09-29 36 views
0

paddingのタイトルはAlertDialog.Builderというタイトルで出ていますが、私はcenterにタイトルを入れたいので、私はsetCustomTitleを使用していますが、マージンとそのパディングにはまっています。私は表示されている不要なパディングと、タイトルに上端を設定したい、私はLinearLayout.LayoutParamsを使用していますが、効果はありません。処理するために何をすべきかを提案してくださいit.thanksAlertDialogカスタムタイトルの上余白を設定し、不要な埋め込みを削除するにはどうすればよいですか?

コード:

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

いいね!パディングに問題はありません。重力コンセプトを使用して 'setContentView()'に 'dialog'で欲しいなら**テキストを中心に置くことができます! –

+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メソッドで使用して、共通のフラグメントを作成する方法でダイアログの独自の表示を作成してください。適切なダイアログを作成するより良い方法です。

0

私はあなたがその非常に簡単に、アラートダイアログとしてカスタムレイアウトを表示するために、我々はそれについての詳細requirement..forごとに私たちのダイアログをカスタマイズすることができDialogFragmentを使用することをお勧め: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> 
関連する問題