2016-06-21 9 views
0

私のAlertDialogでは、ポジティブボタンとネガティブボタンが「接続」されています。私は彼らの間にギャップがあるはずだと確信しています。誰かがなぜこれが起こっているのか教えてもらえますか?私はどんなコードを提供してもうれしいです。 Here is what my AlertDialog looks like.2つのAlertDialogボタンにギャップがないのはなぜですか?

私は、MainActivityでは(私はそれが必要だとは思わないので、そのXMLコードを投稿しますが、私に知らせません。)本体のカスタムビューだけでなく、私のAlertDialogのタイトルを持っています私のカスタムタイトルとボディビューを膨張させ、setPositive()とsetNegative()をオーバーライドすると、onShow()を使用してボタンの色をカスタマイズします。

ご迷惑をおかけして申し訳ありませんが、助けていただければ幸いです。ここに私のMainActivityは次のとおりです。

public void openPrompt(View view){ 
    //builds and opens custom view with prompt.XML 
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this); 
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
    EditText input = (EditText)promptView.findViewById(R.id.userInput); 

    builder.setCancelable(true).setView(R.layout.customdialoglayout) 
    .setNegativeButton("One", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(MainActivity.this,"CANCEL clicked",Toast.LENGTH_SHORT).show(); 
     } 
    }) 
    .setPositiveButton("Two", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      Toast.makeText(MainActivity.this,"SET clicked",Toast.LENGTH_SHORT).show(); 
     } 
    }); 


    //set title with custom XML layout view 
    LayoutInflater inflater = getLayoutInflater(); 
    View titleView = inflater.inflate(R.layout.cutomtitlebar,null); 
    builder.setCustomTitle(titleView); 

    AlertDialog ad = builder.create(); 

    //change colors of background and buttons 
    ad.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 

      Context context = MainActivity.this; 
      Window view = ((AlertDialog)dialog).getWindow(); 

      view.setBackgroundDrawableResource(R.color.colorPrompt); 
      Button negButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE); 
      negButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton)); 
      negButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText)); 

      Button posButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE); 
      posButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton)); 
      posButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText)); 
     } 
    }); 


    ad.show(); 


} 

EDITここで私は()setviewコマンドを使用私のXMLです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="CUSTOM TEXT" 
    android:id="@+id/textView3" 
    android:layout_gravity="center"/> 

+2

あなたにレイアウトスペースを追加することができますので、layout.xmlをご覧ください。 – HCarrasko

+0

あなたの質問にAndroidスタジオは何も得られませんでした。関連するタグのみを使用してください。 –

答えて

1

なぜそれらの間にギャップが存在すべきですか?肯定と否定のボタンは、AlertDialogクラスからレイアウトの寸法を取得しますが、私が思い出すことのできるように、ボタンの間に余白がありません。

余白を追加するには、独自のボタンを作成し、AlertDialogの肯定および否定ボタンを使用しないでください。また、ボタンにスタイルを付けたのと同様の方法でボタンに余白を追加できます。

ad.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 

      Context context = MainActivity.this; 
      Window view = ((AlertDialog)dialog).getWindow(); 

      view.setBackgroundDrawableResource(R.color.colorPrompt); 
      Button negButton = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE); 
      negButton.setBackgroundColor(context.getResources().getColor(R.color.colorPromptButton)); 
      negButton.setTextColor(context.getResources().getColor(R.color.colorPromptButtonText)); 

      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT 
      ); 
      params.setMargins(20,0,0,0); 
      negButton.setLayoutParams(params); 
     } 
    }); 
+0

うわー、うまくいった。私はそれが問題だと思っていましたが、 "デフォルト"のAlertDialogを使用すると、ボタン間にギャップがあり、何か間違っていると思っていました。ありがとうございました! –

+0

偉大な答え! 「なぜそれらの間にギャップがあるべきですか? 長いテキストを使用した場合、「はい」、「いいえ」、またはRTL言語間にギャップがないため – user2396640

関連する問題