Android scrollview无法正常工作 - TableLayout

时间:2016-09-10 03:22:13

标签: android android-tablelayout android-relativelayout

我正在以编程方式向TableLayout添加组件,但滚动不起作用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <ScrollView
        android:id="@+id/appointments_view"
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <TextView
                android:id="@+id/appointmentType_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:text="@string/text_appointment_type"
                android:background="@color/text_grey_light"
                android:padding="@dimen/list_item_top_padding"
                android:textColor="@color/text_grey"/>

            <Spinner
                android:id="@+id/appointmentTypes"
                android:layout_width="wrap_content"
                android:padding="@dimen/booking_text_padding"
                android:layout_below="@+id/appointmentType_title"
                android:background="@null"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/appointmentTypeDescription"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="false"
                android:textSize="16sp"
                android:layout_below="@+id/appointmentTypes"
                android:paddingStart="28dp"
                android:paddingEnd="@dimen/list_item_top_padding"
                android:textColor="@color/text_grey_light"/>

            <TextView
                android:id="@+id/select_appointments_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:layout_below="@id/appointmentTypeDescription"
                android:textStyle="bold"
                android:text="@string/text_appointment"
                android:background="@color/text_grey_light"
                android:layout_marginTop="@dimen/list_item_top_padding"
                android:padding="@dimen/list_item_top_padding"
                android:textColor="@color/text_grey"/>


            <TextView
                android:id="@+id/no_appointments_available"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:layout_below="@id/select_appointments_title"
                android:text="@string/text_no_appointment"
                android:layout_centerHorizontal="true"
                android:padding="@dimen/list_item_top_padding"
                android:textColor="@color/text_grey"/>

            <Spinner
                android:id="@+id/appointment_days"
                android:layout_width="wrap_content"
                android:padding="@dimen/booking_text_padding"
                android:background="@null"
                android:layout_below="@id/select_appointments_title"
                android:layout_height="wrap_content" />

              <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/table"
                  android:layout_below="@id/appointment_days"
                android:paddingStart="@dimen/booking_text_padding"
                android:paddingEnd="@dimen/booking_text_padding">
             </TableLayout>

         </RelativeLayout>

    </ScrollView>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/action_book"
        style="?android:textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/action_booking"
        android:textStyle="bold"
        android:background="@color/colorPrimary"
        android:textColor="@color/white"
        android:layout_alignParentBottom="true"/>

    </RelativeLayout>

向tableLayout container

添加组件
private void addComponents(List<String> values){
        for(String value: values){

                TableRow tr = new TableRow(getActivity());
                TableRow.LayoutParams p1 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
                tr.setPadding(getValueInDp(0),getValueInDp(8),getValueInDp(0),getValueInDp(0));
                tr.setLayoutParams(p1);



            TextView tv = new TextView(getActivity());
            tv.setText(value);
            int padding = getValueInDp(10);
            tv.setBackground(ContextCompat.getDrawable(getContext(), R.drawable.rounded_time_view));
            tv.setTextColor(ContextCompat.getColor(getContext(), R.color.colorPrimary));
            tv.setId(View.generateViewId());
            TableRow.LayoutParams p = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
            p.setMarginStart(getValueInDp(25));
            tv.setLayoutParams(p);
            tv.setClickable(true);
            tv.setOnClickListener(valueClicked);
            tv.setPadding(padding,padding,padding,padding);
            tr.addView(tv);
            container.addView(tr);
        }

    }

0 个答案:

没有答案