Tablelayout和tablerow未正确设置

时间:2017-02-04 15:38:38

标签: android android-fragments android-tablelayout

我有一个代码,其中我有一个TableLayout,其中一个TableRow有三个textview。当我将数据从类传递到此片段类时,数据无法在表上正确显示。此外,我设置了水平视图,显示Android Studio上的大小正在变化,但手机上没有变化。

XML文件

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


        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/home_fragment">

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

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="112dp"
                            android:paddingBottom="5dp"
                            android:src="@drawable/the_one"
                            android:id="@+id/the_one"
                            />

                        <ImageView
                            android:layout_width="match_parent"
                            android:layout_height="1000dp"
                            android:src="@drawable/the_upperblank"
                            android:layout_below="@id/the_one"
                            android:background="#ffffff"
                            android:id="@+id/the_upperblank"
                            />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="3px"
                            android:id="@+id/view_above_linear3"
                            android:layout_above="@+id/linear_layout_one_training_history"
                            android:background="#ebebeb"/>

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="40dp"
                                android:gravity="center"
                                android:layout_below="@id/the_one"
                                android:orientation="horizontal"
                                android:id="@+id/linear_layout_one_training_history"
                                >

                                <ImageView
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    android:src="@drawable/the_date"
                                    android:gravity="center"
                                    android:layout_weight="1"
                                    android:id="@+id/the_date" />

                                <ImageView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:src="@drawable/the_vertical"
                                    />


                                <ImageView
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    android:src="@drawable/the_session_time"
                                    android:layout_weight="1"
                                    android:gravity="center"
                                    android:id="@+id/the_session_time"/>

                                <ImageView
                                    android:layout_width="wrap_content"
                                    android:layout_height="60dp"
                                    android:src="@drawable/the_vertical"
                                    />


                                <ImageView
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    android:src="@drawable/the_average_speed"
                                    android:layout_weight="1"
                                    android:gravity="center"
                                    android:id="@+id/the_average_speed"/>
                            </LinearLayout>

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="20px"
                        android:id="@+id/view_below_linear3"
                        android:layout_below="@id/linear_layout_one_training_history"
                        android:background="#ebebeb"/>

                    <TableLayout
                        android:id="@+id/maintable"
                        android:layout_below="@id/view_below_linear3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <TableRow
                            android:id="@+id/tableRow1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            >

                            <TextView
                                android:id="@+id/date_training_history"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:visibility="invisible"
                                android:textStyle="bold"
                                android:gravity="center"
                                android:layout_weight="1"
                                android:textColor="#000000" />

                            <TextView
                                android:id="@+id/session_time_training_history"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:visibility="invisible"
                                android:gravity="center"
                                android:layout_weight="1"
                                android:textColor="#b2b2b2" />

                            <TextView
                                android:id="@+id/avg_speed_training_history"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:visibility="invisible"
                                android:gravity="center"
                                android:layout_weight="1"
                                android:textColor="#b2b2b2" />
                        </TableRow>
                    </TableLayout>
                </RelativeLayout>
            </FrameLayout>
        </ScrollView>
    </RelativeLayout>

JAVA文件

        package com.example.spuck.fragment;

    import android.content.Context;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.util.Log;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;

    import com.example.spuck.R;
    import com.google.api.services.people.v1.People;

    import java.sql.Time;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.List;

    import static android.support.design.R.id.center;

    /**
     * A simple {@link Fragment} subclass.
     * Activities that contain this fragment must implement the
     * {@link TrainingHistory.OnFragmentInteractionListener} interface
     * to handle interaction events.
     * Use the {@link TrainingHistory#newInstance} factory method to
     * create an instance of this fragment.
     */
    public class TrainingHistory extends Fragment {
        // TODO: Rename parameter arguments, choose names that match
        // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
        private static final String ARG_PARAM1 = "param1";
        private static final String ARG_PARAM2 = "param2";

        // TODO: Rename and change types of parameters
        private String mParam1;
        private String mParam2;

        private OnFragmentInteractionListener mListener;

        // ----------------------------- //
        View view;
        String[] START_DATE_STRING_LIST;
        String[] START_TIME_ARRAY_LIST;
        String[] END_TIME_ARRAY_LIST;
        int[] AVERAGE_SPEED;
        long[] SESSION_TIME;
        TableRow tableRow;

        TextView date, avg_speed, session_time;

        // ----------------------------- //
        public TrainingHistory() {
            // Required empty public constructor
        }

        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param param1 Parameter 1.
         * @param param2 Parameter 2.
         * @return A new instance of fragment TrainingHistory.
         */
        // TODO: Rename and change types and number of parameters
        public static TrainingHistory newInstance(String param1, String param2) {
            TrainingHistory fragment = new TrainingHistory();
            Bundle args = new Bundle();
            args.putString(ARG_PARAM1, param1);
            args.putString(ARG_PARAM2, param2);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null) {
                mParam1 = getArguments().getString(ARG_PARAM1);
                mParam2 = getArguments().getString(ARG_PARAM2);
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            view = inflater.inflate(R.layout.fragment_training_history, container, false);
            date = (TextView) view.findViewById(R.id.date_training_history);
            avg_speed = (TextView) view.findViewById(R.id.avg_speed_training_history);
            session_time = (TextView) view.findViewById(R.id.session_time_training_history);
            tableRow = (TableRow) view.findViewById(R.id.tableRow1);
            return view;
        }

        // TODO: Rename method, update argument and hook method into UI event
        public void onButtonPressed(Uri uri) {
            if (mListener != null) {
                mListener.onFragmentInteraction(uri);
            }
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

            Bundle b = this.getArguments();
            START_DATE_STRING_LIST = b.getStringArray("start_date");
            START_TIME_ARRAY_LIST = b.getStringArray("start_time");
            END_TIME_ARRAY_LIST = b.getStringArray("end_time");
            AVERAGE_SPEED = b.getIntArray("speed");
            SESSION_TIME = b.getLongArray("session_time");
            TableLayout tl=(TableLayout) view.findViewById(R.id.maintable);

            for (int i=0;i<AVERAGE_SPEED.length;i++)
            {
                date = new TextView(getContext());
                avg_speed = new TextView(getContext());
                session_time = new TextView(getContext());

                date.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
                avg_speed.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));
                session_time.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));

                date.setGravity(Gravity.CENTER);
                avg_speed.setGravity(Gravity.CENTER);
                session_time.setGravity(Gravity.CENTER);

                date.setBackgroundResource(R.drawable.cell_shape);
                avg_speed.setBackgroundResource(R.drawable.cell_shape);
                session_time.setBackgroundResource(R.drawable.cell_shape);

                /*date.setTextSize(16);
                avg_speed.setTextSize(16);
                session_time.setTextSize(16);*/


                tableRow = new TableRow(getContext());
                tableRow.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));

                DateFormat df = new SimpleDateFormat("hh:mm:ss");
                Date date1 = new Date(0);
                Date date2 = new Date(0);

                if (SESSION_TIME[i] == 0)
                {
                        session_time.setText("00:00:00");
                }

                else
                {
                    try {
                        long diff = 0;
                        date1 = df.parse(START_TIME_ARRAY_LIST[i]);
                        date2 = df.parse(END_TIME_ARRAY_LIST[i]);
                        diff = date2.getTime() - date1.getTime();
                        Log.i("inside traininghistory", " value of date1 is " + date1);
                        Log.i("inside traininghistory", " value of date2 is " + date2);

                        int timeInSeconds = (int) (diff / 1000);
                        int hours, minutes, seconds;
                        hours = timeInSeconds / 3600;
                        timeInSeconds = timeInSeconds - (hours * 3600);
                        minutes = timeInSeconds / 60;
                        timeInSeconds = timeInSeconds - (minutes * 60);
                        seconds = timeInSeconds;

                        session_time.setText(hours + ":" + minutes + ":" + seconds);

                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }

                date.setText("" + START_DATE_STRING_LIST[i]);
                avg_speed.setText("" + AVERAGE_SPEED[i]);

                tableRow.addView(date);
                tableRow.addView(session_time);
                tableRow.addView(avg_speed);

                tl.addView(tableRow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
            }


        }


        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
           /* if (context instanceof OnFragmentInteractionListener) {
                mListener = (OnFragmentInteractionListener) context;
            } else {
                throw new RuntimeException(context.toString()
                        + " must implement OnFragmentInteractionListener");
            }*/
        }

        @Override
        public void onDetach() {
            super.onDetach();
            mListener = null;
        }

        /**
         * This interface must be implemented by activities that contain this
         * fragment to allow an interaction in this fragment to be communicated
         * to the activity and potentially other fragments contained in that
         * activity.
         * <p>
         * See the Android Training lesson <a href=
         * "http://developer.android.com/training/basics/fragments/communicating.html"
         * >Communicating with Other Fragments</a> for more information.
         */
        public interface OnFragmentInteractionListener {
            // TODO: Update argument type and name
            void onFragmentInteraction(Uri uri);
        }
    }

0 个答案:

没有答案
相关问题