在 AlertDialog 中向 LinearLayout 添加多行

Posted

技术标签:

【中文标题】在 AlertDialog 中向 LinearLayout 添加多行【英文标题】:Add multiple lines to a LinearLayout in AlertDialog 【发布时间】:2020-11-03 16:46:32 【问题描述】:

在我的应用程序中,我有一个按钮,当按下该按钮时,会显示一个 AlertDialog,其中包含添加带有两个 NumberPicker 的条目的问题,一个用于分钟,另一个用于秒。我想在第一分钟上方显示一个带有“Min”的 TextView,在第二个上方显示一个带有“Sec”的 TextView。 Java 文件如下所示:

//create linearlayout for display on the alertdialog
LinearLayout parent = new LinearLayout(this);
parent.setOrientation(LinearLayout.HORIZONTAL);

//create upper and lower LinearLayout
LinearLayout upper = new LinearLayout(this);
LinearLayout lower = new LinearLayout(this);
upper.setOrientation(LinearLayout.HORIZONTAL);
lower.setOrientation(LinearLayout.HORIZONTAL);

//create parameters for the lower and upper linearLayouts
LinearLayout.LayoutParams upParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams downParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT);

//Create numberPicker and TextView for the Seconds
final NumberPicker secondsPicker = new NumberPicker(this);
secondsPicker.setMinValue(MIN_SECOND_INPUT);
secondsPicker.setMaxValue(MAX_SECOND_INPUT);

final TextView secondsText = new TextView(this);
secondsText.setText(R.string.Seconds);

//Create numberpicker and TextView for the minutes
final NumberPicker minutesPicker = new NumberPicker(this);
minutesPicker.setMinValue(MIN_MINUTE_INPUT);
minutesPicker.setMaxValue(MAX_MINUTE_INPUT);
final TextView minutesText = new TextView(this);
minutesText.setText(R.string.Minutes);

//create parameters for the LinearLayout
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;

//create parameters for the TextView for seconds
LinearLayout.LayoutParams paramsTextSec = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
paramsTextSec.weight = 1;

//create parameters for the TextView for minutes
LinearLayout.LayoutParams paramsTextMin = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
paramsTextMin.weight = 1;

//create parameters for the numberpicker for the seconds
LinearLayout.LayoutParams paramsSeconds = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
paramsSeconds.weight = 1;

//create parameters for the numberpicker for the Minutes
LinearLayout.LayoutParams paramsMinutes = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
paramsMinutes.weight = 1;


//add parameters to linear layout
parent.setLayoutParams(params);
upper.setLayoutParams(upParams);
lower.setLayoutParams(downParams);
upper.addView(secondsPicker, paramsSeconds);
upper.addView(secondsText, paramsTextSec);
lower.addView(minutesPicker, paramsMinutes);
lower.addView(minutesText, paramsTextMin);
parent.addView(upper);
parent.addView(lower);

//create alertdialog with the linear layout with numberpickers
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add an extra slot:");
builder.setView(parent);
builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() 
    public void onClick(DialogInterface dialogInterface, int i) 
        int seconds = secondsPicker.getValue() + minutesPicker.getValue() * 60;
        addValueToScheme(seconds);
   
);
AlertDialog dialog = builder.create();
dialog.show();

AlertDialog 给出的结果是 Seconds 出现在 NumberPicker 旁边,NumberPickers 出现在彼此下方。

如何将 NumberPickers 添加到彼此旁边,并在它们上方添加文本?​​

【问题讨论】:

【参考方案1】:

首先:请将此 Java 代码移到 XML 中,这样会更容易阅读、设计和开发...

upParamsdownParams 添加weight=1

LinearLayout.LayoutParams upParams = new LinearLayout.LayoutParams(
    0, LinearLayout.LayoutParams.MATCH_PARENT);
upParams.weight = 1;
LinearLayout.LayoutParams downParams = new LinearLayout.LayoutParams(
    0, LinearLayout.LayoutParams.MATCH_PARENT);
downParams.weight = 1;

这将导致LinearLayout upper 位于左侧,LinearLayout lower 位于右侧(感谢parent 方向HORIZONTAL),因此请更改它们的名称...然后将它们的方向设置为VERTICAL

upper.setOrientation(LinearLayout.VERTICAL);
lower.setOrientation(LinearLayout.VERTICAL);

现在你有两个垂直容器,左右,宽度相等,你可以添加一些元素,例如首先是TextView,然后是NumberPicker,两者的宽度都是MATCH_PARENT(整个宽度的一半Dialog

【讨论】:

以上是关于在 AlertDialog 中向 LinearLayout 添加多行的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Flutter 中将 AlertDialog FlatButton 居中

如何在 Flutter 中刷新 AlertDialog?

如何在Click上修复错误的alertDialog?

请问在AlertDialog中如何再此嵌入AlertDialog

AlertDialog 更改片段中的背景颜色 [重复]

从 AlertDialog 中调用 SnackBar