如何以编程方式设置对话重力?

时间:2017-05-26 11:21:11

标签: android android-alertdialog

我可以在布局参数中设置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;

2 个答案:

答案 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();

答案 1 :(得分:0)

试试这个......

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