Android AlertDialog按钮定位
Android AlertDialog button positioning
我需要创建一个 AlertDialog
,它垂直分为两个区域:
- 其中一个区域(在右侧)的底部应该有对话框按钮(确定、取消和设置),并且在这些按钮的顶部有另一个布局。
- 此对话框的左侧应该有另一个布局,例如图像。
问题是,是否可以有类似的东西,或者所有的 alertDialogs 都必须将所有按钮都放在底部?我什至不知道从哪里开始。我试过查看 AlertDialog
class,但可以找到任何合适的内容。
您可以创建自己的自定义对话框布局 - 通过以下 3 个步骤:
- 创建自定义对话框布局(XML 文件)。
- 将布局附加到对话框。
- 显示对话框。
看here.
看起来你不需要 AlertDialog
,但是你可以用 AlertDialog
来完成。创建您的视图,然后使用 AlertDialog.Builder
而无需调用 setPositiveButton()
、setNegativeButton()
等。
AlertDialog alertDialog = new AlertDialog.Builder(context)
.setView(yourContentView)
.create();
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity,
R.style.MyAlertDialogStyle);
builder.setTitle(mActivity.getResources().getString(R.string.app_name));
builder.setCancelable(false);
builder.setMessage(str_message);
builder.setPositiveButton(str_btn1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (listener != null) {
listener.onClick(null);
}
}
});
if (str_btn2 != null) {
builder.setNegativeButton(str_btn2,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
if (listener2 != null) {
listener2.onClick(null);
}
}
});
}
builder.show();
编辑:请看上图。你会得到这样的输出。只需创建对话框样式
styles.xml
中的对话框样式
<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <!-- Used for the buttons -->
<item name="android:textStyle">bold</item>
<item name="colorAccent">@android:color/holo_blue_bright</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@android:color/black</item>
<!-- Used for the background -->
<item name="android:background">@android:color/white</item>
</style>
恐怕我来不及了,但一个非常简单的解决方案是:
假设您知道将按钮设置为 AlertDialog
的两种方法,我将省略这部分:
…
yourDialog.show();
Button btn = yourDialog.getButton(DialogInterface.BUTTON_NEUTRAL);//Whatever button it is.
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) btn.getLayoutParams();
params.topMargin = yourValue;//Enter your desired margin value here.
params.bottomMargin = yourValue;
params.leftMargin = yourValue;
params.rightMargin = yourValue;
btn.setLayoutParams(params);
我需要创建一个 AlertDialog
,它垂直分为两个区域:
- 其中一个区域(在右侧)的底部应该有对话框按钮(确定、取消和设置),并且在这些按钮的顶部有另一个布局。
- 此对话框的左侧应该有另一个布局,例如图像。
问题是,是否可以有类似的东西,或者所有的 alertDialogs 都必须将所有按钮都放在底部?我什至不知道从哪里开始。我试过查看 AlertDialog
class,但可以找到任何合适的内容。
您可以创建自己的自定义对话框布局 - 通过以下 3 个步骤:
- 创建自定义对话框布局(XML 文件)。
- 将布局附加到对话框。
- 显示对话框。
看here.
看起来你不需要 AlertDialog
,但是你可以用 AlertDialog
来完成。创建您的视图,然后使用 AlertDialog.Builder
而无需调用 setPositiveButton()
、setNegativeButton()
等。
AlertDialog alertDialog = new AlertDialog.Builder(context)
.setView(yourContentView)
.create();
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity,
R.style.MyAlertDialogStyle);
builder.setTitle(mActivity.getResources().getString(R.string.app_name));
builder.setCancelable(false);
builder.setMessage(str_message);
builder.setPositiveButton(str_btn1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (listener != null) {
listener.onClick(null);
}
}
});
if (str_btn2 != null) {
builder.setNegativeButton(str_btn2,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface,
int i) {
if (listener2 != null) {
listener2.onClick(null);
}
}
});
}
builder.show();
编辑:请看上图。你会得到这样的输出。只需创建对话框样式
styles.xml
中的对话框样式 <style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert"> <!-- Used for the buttons -->
<item name="android:textStyle">bold</item>
<item name="colorAccent">@android:color/holo_blue_bright</item>
<!-- Used for the title and text -->
<item name="android:textColorPrimary">@android:color/black</item>
<!-- Used for the background -->
<item name="android:background">@android:color/white</item>
</style>
恐怕我来不及了,但一个非常简单的解决方案是:
假设您知道将按钮设置为 AlertDialog
的两种方法,我将省略这部分:
…
yourDialog.show();
Button btn = yourDialog.getButton(DialogInterface.BUTTON_NEUTRAL);//Whatever button it is.
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) btn.getLayoutParams();
params.topMargin = yourValue;//Enter your desired margin value here.
params.bottomMargin = yourValue;
params.leftMargin = yourValue;
params.rightMargin = yourValue;
btn.setLayoutParams(params);