【问题标题】:With AlertDialog, how to align the top of the choice text with the checkbox?使用 AlertDialog,如何将选择文本的顶部与复选框对齐?
【发布时间】:2021-08-08 08:24:44
【问题描述】:

我正在使用 AlertDialog 的 setMultiChoiceItems。复选标记位于选择文本的垂直中心。我希望它位于顶部。该怎么做?

(Kotlin 代码。Java 答案会很好。)

        AlertDialog.Builder (this).run {
            val choices = arrayOf(
                "Option 1 is very long. It is more than 3 lines. I would like to align the text top with the check box top.",
                "Option 2 is very long. It is more than 3 lines. I would like to align the text top with the check box top.",
                "Option 3 is very long. It is more than 3 lines. I would like to align the text top with the check box top.",
           )
            val booleanSelections = booleanArrayOf(true, true, true)
            setMultiChoiceItems(choices, booleanSelections, null)
            show()
        }

【问题讨论】:

  • 确实如此。这是一个麻烦。希望你能得到解决方案。物品周围的框架也很好。

标签: java android kotlin android-layout kotlin-android-extensions


【解决方案1】:

您可以像这样在这个多选对话框中添加自定义布局。

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setSingleChoiceItems(choice, 0, null);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),
                R.layout.item_text_layout, choices);
        builder.setAdapter(arrayAdapter, null);

AlertDialog alertDialog = builder.create();

ListView listView = alertDialog.getListView();
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
            R.layout.item_text_layout, choices));
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckedTextView checkedTextView = (CheckedTextView) view;
            checkedItems[position] = !checkedTextView.isChecked();
        }
    });

alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialog) {
            setCheckedItems(((AlertDialog) dialog).getListView());
        }
    });

alertDialog.show();

然后处理

【讨论】:

  • 如果我正确理解您实际上是在建议创建我自己的检查项,而忽略 AlertDialog 的此功能。所以,2个问题。一,这里与顶部的实际对齐在哪里?二,这是您的建议,因为据您所知,没有办法与 AlertDialog 布局对齐?
  • 你试过了吗?您是否正确对齐了项目?
猜你喜欢
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 2019-11-13
  • 2016-12-07
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 2016-06-04
相关资源
最近更新 更多