Android AlertDialog Move PositiveButton to the right and NegativeButton on the left

后端 未结 7 1968
南方客
南方客 2021-01-07 23:11

I\'m new with android.

Currently I want to show an AlertDialog box with \'OK\' & \'Cancel\' buttons.

The default is PositiveButton: Left, Ne

7条回答
  •  梦毁少年i
    2021-01-07 23:52

    A really simple way to shift the buttons of the AlertDialog to the right hand side is to hide the leftSpacer, a LinearLayout within the core XML which handles the default layout.

    // Fetch the PositiveButton
    final Button       lPositiveButton = lDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    // Fetch the LinearLayout.
    final LinearLayout lParent         = (LinearLayout) lPositiveButton.getParent();
    // Ensure the Parent of the Buttons aligns it's contents to the right.
    lParent.setGravity(Gravity.RIGHT);
    // Hide the LeftSpacer. (Strict dependence on the order of the layout!)
    lParent.getChildAt(1).setVisibility(View.GONE);
    

提交回复
热议问题