2017-05-26 67 views
0

我可以在我的佈局參數中設置WRAP_CONTENT,但是如何設置CENTER重力?如何以編程方式設置對話重力?

final AlertDialog.Builder popDialog = new AlertDialog.Builder(this); 
    final RatingBar rating = new RatingBar(this); 
    rating.setMax(5); 
    rating.setRating(1); 
    rating.setNumStars(5); 
    popDialog.setIcon(android.R.drawable.btn_star_big_on); 
    popDialog.setTitle("Vote!! "); 
    popDialog.setView(rating); 
    popDialog.setPositiveButton(android.R.string.ok, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        int rawrating = rating.getProgress(); 
        dialog.dismiss(); 
       } 
      }) 
      .setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
          dialog.cancel(); 
         } 
        }); 
    popDialog.create(); 
    popDialog.show(); 
    // force to set setNumStars 
    rating.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT; 
+0

你想中心對話框位置或對話有何看法? –

+0

我想在我的alertDialog中心RatingBar – user8069029

+0

您可以爲警報對話框進行自定義佈局,並且在該自定義佈局中,您可以將您的評級欄的重力設置爲任何所需的值 –

回答

1

試試這個會幫助你。

像瞭解創建這個

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center" 
android:orientation="vertical"> 
    <RatingBar 
    android:id="@+id/ratingbar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:max="5" 
    android:numStars="5" 
    android:stepSize="1" /> 
</LinearLayout> 

    final AlertDialog.Builder popDialog = new AlertDialog.Builder(MainActivity.this); 
    popDialog.setIcon(android.R.drawable.btn_star_big_on); 
    popDialog.setTitle("Vote!! "); 
    popDialog.setView(R.layout.custom); 
    popDialog.setCancelable(false); 
    LayoutInflater mlLayoutInflater=LayoutInflater.from(MainActivity.this); 
    final View dialView=mlLayoutInflater.inflate(R.layout.custom,null); 
    final RatingBar ratingBar = (RatingBar) dialView.findViewById(R.id.ratingbar); 
    AlertDialog dialog_card = popDialog.create(); 
    Window window = dialog_card.getWindow(); 

    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); 
    window.setGravity(Gravity.CENTER); 


    popDialog.setPositiveButton(android.R.string.ok, 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        int rawrating = ratingBar.getProgress(); 
        Toast.makeText(MainActivity.this, "Rating :-> " + rawrating, Toast.LENGTH_SHORT).show(); 

       } 
      }) 
      .setNegativeButton("Cancel", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 

         } 
        }); 

    popDialog.show(); 
0

自定義視圖試試這個...

 Window window = dialog_card.getWindow(); 
     window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT); 
     window.setGravity(Gravity.CENTER); 
+0

你試過了嗎? – user8069029

相關問題