我仅使用代码创建AlertDialog并插入复选框视图。但是检查的重力表现却很奇怪。
如何将text + box放在中间,或者至少将文本放在左侧,并以填充开头。
谢谢。
private void openFilterDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
builder.setTitle(getString(R.string.filter));
// Set up the input
final CheckBox hideNoResponseBox = new CheckBox(getContext());
//Put the checkBox on the right side
hideNoResponseBox.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
hideNoResponseBox.setText(R.string.hide_no_responses);
hideNoResponseBox.setChecked(hideNoResponse);
hideNoResponseBox.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
hideNoResponseBox.setGravity(View.TEXT_ALIGNMENT_CENTER);
builder.setView(hideNoResponseBox);
// Set up the buttons
builder.setPositiveButton(getString(R.string.do_filter), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Utils.isDoubleClick()) return;
hideNoResponse = hideNoResponseBox.isChecked();
showLoadingDialog(true);
hideListItem(adapter.getDistanceArrayList());
dialog.dismiss();
}
});
builder.setNegativeButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
答案 0 :(得分:1)
从setGravity()
类继承的CheckBox的View
方法采用Gravity
常量作为值。
https://developer.android.com/reference/android/view/Gravity
编辑:
您可以尝试使用布局参数设置文本重力。 AlertDialog的LayoutParams是LinearLayout的LayoutParams,如下所示:What LayoutParams should be used in an AlertDialog?
您只需要使用LayoutParam的setGravity()
方法。
关于填充,您可以尝试将CheckBox的父对象强制转换为ViewGroup并在其中设置填充,如下所示:
答案 1 :(得分:0)
private void openFilterDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(Objects.requireNonNull(getContext()));
builder.setTitle(getString(R.string.filter));
// Set up the input
final CheckBox hideNoResponseBox = new CheckBox(getContext());
//Put the checkBox on the right side
hideNoResponseBox.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
hideNoResponseBox.setText(R.string.hide_no_responses);
hideNoResponseBox.setChecked(hideNoResponse);
hideNoResponseBox.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
hideNoResponseBox.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
hideNoResponseBox.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_START);
int px = Math.round(TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 16, getResources().getDisplayMetrics()));
hideNoResponseBox.setPadding(px,0,0,0);
builder.setView(hideNoResponseBox);
// Set up the buttons
builder.setPositiveButton(getString(R.string.do_filter), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (Utils.isDoubleClick()) return;
hideNoResponse = hideNoResponseBox.isChecked();
showLoadingDialog(true);
hideListItem(adapter.getDistanceArrayList());
dialog.dismiss();
}
});
builder.setNegativeButton(getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}