首页 > 解决方案 > 在 AlertDialog 中向 LinearLayout 添加多行

问题描述

在我的应用程序中,我有一个按钮,当按下该按钮时,会显示一个 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 与它们上方的文本并排添加?

标签: javaandroidandroid-layoutandroid-alertdialogandroid-linearlayout

解决方案


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

添加和weight=1_upParamsdownParams

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);

现在你有两个垂直容器,左和右,宽度相等,你可以添加到两个元素,例如 first TextView, after that NumberPicker,两者都具有宽度MATCH_PARENT(整体宽度的一半Dialog


推荐阅读