【问题标题】:Remove spaces on the top and the bottom of the items in AlertDialog删除 AlertDialog 中项目顶部和底部的空格
【发布时间】:2018-11-04 21:02:55
【问题描述】:

我有一个AlertDialog,其中包含ArrayAdapter 提供的项目。当我显示AlertDialog 时,项目的顶部和底部都有空格。我想完全删除它们。当然,更改AlertDialog 背景的颜色以匹配项目的背景可以解决问题,但我想完全删除它。这是所说的空格:

这是 AlertDialog 的样式:(colorPrimaryLight 是空间的颜色)

<style name="style_alert_dialog" parent="Theme.AppCompat.Dialog.Alert">
        <item name="android:padding">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:background">@color/colorPrimaryLight</item>
</style>

用于 ArrayAdapter 的布局(template_alert_dialog_item):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/colorTextWhite"
    android:background="@color/colorPrimaryNormal"
    android:textSize="15sp"
    android:padding="10dp">
</TextView>

我使用的代码:

ArrayAdapter<String> adapter = new ArrayAdapter<>(ReviewMeTooMainPage.this, R.layout.template_alert_dialog_item, 
                        getResources().getStringArray(R.array.menu_items));
                AlertDialog.Builder b = new Builder(ReviewMeTooMainPage.this, R.style.style_alert_dialog);
                View inflatedTemplate = getLayoutInflater().inflate(R.layout.template_textview,(
                        ViewGroup) findViewById(R.id.main_page_display),false);
                TextView textView = inflatedTemplate.findViewById(R.id.template_title);
                b.setCustomTitle(textView);

                b.setAdapter(adapter, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });

                b.show();

我尝试使用:

AlertDialog dialog = b.show();
                dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

但没有占上风。

如何去除这些边框?

【问题讨论】:

  • 请尝试将边距也设置为 0 )
  • 可以显示 layout.template_textview 文件吗?
  • 你的template_textview.xml可能有边距或内边距
  • 这可能会有所帮助:stackoverflow.com/questions/5329272/…
  • @Tepits 我没用过。

标签: java android android-alertdialog


【解决方案1】:

如果您使用自定义布局,此填充将消失。这是因为使用了setAdapter。因为它在 AlertDialog 构建器中使用默认的 ListView。因此,您可以将 ListView 放在内容布局中并将其设置为 AlertDialog,如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View viewInflated = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_people_info, (ViewGroup) mRootView, false);

ListView personListView = viewInflated.findViewById(R.id.people_listView);
ListAdapter listAdapter = new PersonAdapter(getActivity(), R.layout.person_lsit_item, personsInfo);
personListView.setAdapter(listAdapter);

builder.setView(viewInflated);
builder.setNegativeButton(R.string.action_close, (dialog, which) -> dialog.cancel());

AlertDialog dialog = builder.create();
dialog.show();

【讨论】:

  • 这可以解决问题。我将代码更改为使用setView() and use ListView.
【解决方案2】:

删除:

android:padding="10dp"

TextView 中可能会有所帮助,因为在您的情况下填充在内容中(TextView)。

如果没有帮助,请尝试删除它:

<item name="android:padding">0dp</item>

【讨论】:

  • 如果是这样的话,那应该出现在左右两边吧?
  • @Kannan_SJD 是的,我删除了它,它仍然存在。