在DialogFragment上以编程方式添加视图会留下空白空间

时间:2016-11-17 17:33:11

标签: java android android-layout android-databinding

鉴于我有一个从DialogFragment扩展的自定义对话框类。 假设我有一个显示医生信息的对话框,它还有预定时间表,包括显示可用日期的String ArrayList。

在我的布局中,我为图像,名称准备了一个占位符,并且我还准备了一个垂直的线性布局,我将以编程方式从Java类中添加ArrayView。

我正在循环List以将addView添加到LinearLayout

    private Doctor doctor; // it has doctor.schedule = ArrayList<String>

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LayoutSearchDoctorDialogBinding binding = DataBindingUtil.inflate(inflater, R.layout.layout_search_doctor_dialog, container, true);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        binding.layoutSchedule.removeAllViewsInLayout();
        for (Schedule schedule : doctor.schedule) {
            View view = inflater.inflate(R.layout.item_schedule, null);
            TextView textDays = (TextView) view.findViewById(R.id.text_days);
            TextView textHours = (TextView) view.findViewById(R.id.text_hours);
            textDays.setText(schedule.day);
            textHours.setText(schedule.hours);
            binding.layoutSchedule.addView(view,
                    new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
        }
        binding.setViewModel(new SearchDoctorDialogViewModel(doctor));
        binding.btnCall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((SearchDoctorView) getActivity()).checkRecords(doctor.phoneNumber);
                dismiss();
            }
        });
        return binding.getRoot();
    }

这是我的对话框布局 R.layout.layout_search_doctor_dialog

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/dimen_16dp">

    <com.bloonerd.rspondokindah.custom.CircleImageView
        android:layout_width="@dimen/dimen_60dp"
        android:layout_height="@dimen/dimen_60dp"
        android:layout_marginTop="@dimen/dimen_20dp"
        android:layout_marginBottom="@dimen/dimen_20dp"
        android:layout_gravity="center"
        app:imageUrl="@{viewModel.image}"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        tools:text="Michael Jackson"
        android:text="@{viewModel.doctorName}"
        android:textStyle="bold"
        android:textColor="@color/black"
        android:ellipsize="end"
        android:textSize="@dimen/dimen_16sp"/>

    <!-- Linear Layout below is where I will addViews -->
    <LinearLayout
        android:id="@+id/layout_schedule"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="@dimen/dimen_16dp"
        android:paddingRight="@dimen/dimen_16dp"/>

</LinearLayout>

这也是我添加到LinearLayout addView R.layout.item_schedule

的内容
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<TextView
        android:id="@+id/text_days"
        style="@style/Width0HeightWrapStyle"
        android:textSize="@dimen/dimen_16sp"
        android:textColor="@color/text_small"/>

    <TextView
        android:id="@+id/text_hours"
        style="@style/WrapContentStyle"
        android:textSize="@dimen/dimen_16dp"
        android:textColor="@color/text_small"/>

</LinearLayout>

当我调试它时,它显示我正在循环 doctor.schedule 并将其添加到视图中。但它只显示String ArrayList的第一个索引。 然而,它在LinearLayout中留下了空格,看起来像String ArrayList已经添加但是已隐藏。

此外,在我使用uiautomatorviewer查看后,layout_schedule LinearLayout

中只有一个子视图

我意识到这只适用于DialogFragment,在RecyclerView中我可以很好地显示列表

现在如何: ----------------------------------- ----------&GT; 我想要什么

__________           __________            __________
|        |  ----->   |[item 1]|   ----->   |[item 1]|
----------   Loop    |        |            |[item 2]|
                     |        |            |[item 3]|
                     |        |            |[item 4]|
                     |        |            |[item 5]|
                     ----------            ----------

0 个答案:

没有答案