在AlertDialog中的项目的顶部和底部删除空格

时间:2018-09-13 06:41:12

标签: java android android-alertdialog

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

enter image description here

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

但不占优势。

如何删除这些边框?

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

正在移除:

android:padding="10dp"

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

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

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