我对使用framelayouts的android中的tablelayouts有疑问。但是,代码可以工作,但是我通过java添加的行不能正确地与表格对齐,在开头包装。
日期 - 1/2/17 会议时间 - 05:00:00 平均速度 - 29
TrainingHistory.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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import com.example.spuck.R;
import java.sql.Time;
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;
int[] POINTS_ARRAY;
int[] AVERAGE_SPEED;
long[] SESSION_TIME;
TableRow tableRow;
TextView date, avg_speed, session_time;
List<String> START_TIME_STRING_LIST;
List<String> END_TIME_STRING_LIST;
// ----------------------------- //
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();
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());
tableRow = new TableRow(getContext());
tableRow.setLayoutParams(new TableRow.LayoutParams( TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
Date d = new Time(SESSION_TIME[i]);
date.setText("1/2/17");
avg_speed.setText("" + AVERAGE_SPEED[i]);
session_time.setText("" + d);
date.setGravity(center);
date.setBackgroundResource(R.drawable.cell_shape);
avg_speed.setBackgroundResource(R.drawable.cell_shape);
session_time.setBackgroundResource(R.drawable.cell_shape);
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);
}
}
我希望textviews与上面的图像Date,Session和speed正确对齐,我该怎么办?
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="107dp"
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"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="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="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>
<TableLayout
android:id="@+id/maintable"
android:layout_below="@id/linear_layout_one_training_history"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:text="Date"
android:visibility="invisible"
android:textSize="16dp"
android:gravity="center"
android:layout_weight="4.6"
android:textColor="#000000" />
<TextView
android:id="@+id/session_time_training_history"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:visibility="invisible"
android:textSize="16dp"
android:text="Session Time"
android:layout_weight="1"
android:textColor="#000000" />
<TextView
android:id="@+id/avg_speed_training_history"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Average Speed"
android:textSize="16dp"
android:visibility="invisible"
android:gravity="center"
android:layout_weight="1"
android:textColor="#000000" />
</TableRow>
</TableLayout>
</RelativeLayout>
</FrameLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:1)
我认为你也想要修复标题行。我建议你使用这个库https://github.com/InQBarna/TableFixHeaders。它会为你节省很多时间。
答案 1 :(得分:1)
在java中动态设置权重为textview,即日期,avg_speed等,如
TextView txtview = new TextView(v.getContext());
LayoutParams params = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
txtview.setLayoutParams(params);
其中1f的权重为1,您可以根据需要提供不同的权重。