所以我有一个for循环,我希望每次在textview中输出一个新行。但是,我不确定这是否可行。我有TableLayout,但它把项目垂直而不是水平放置。我想到了ListView但不知道如何设置它的文本。
这是我的代码
<Window.Resources>
<ControlTemplate x:Key="RbTemplateKey" TargetType="RadioButton">
<ToggleButton IsChecked="{Binding IsChecked, Mode=TwoWay,RelativeSource={RelativeSource AncestorType=RadioButton, Mode=FindAncestor}}"/>
</ControlTemplate>
</Window.Resources>
<Grid>
<StackPanel Margin="141,148,161,0" Orientation="Horizontal" Background="AliceBlue">
<RadioButton Width="25" Height="25" Margin="10" Template="{StaticResource RbTemplateKey}"/>
<RadioButton Width="25" Height="25" Margin="10" Template="{StaticResource RbTemplateKey}"/>
<RadioButton Width="25" Height="25" Margin="10" Template="{StaticResource RbTemplateKey}"/>
<RadioButton Width="25" Height="25" Margin="10" Template="{StaticResource RbTemplateKey}"/>
</StackPanel>
</Grid>
这是我的XML:
import android.app.Fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
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.jamessingleton.chffrapi.com.examples.jamessingleton.chffrapi.data.Route;
import org.joda.time.DateTime;
import org.w3c.dom.Text;
import java.io.IOException;
import java.util.Map;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by James Singleton on 8/13/2016.
*/
public class WeeklyDrives extends Fragment implements APIRequestsUtil.APIRequestResponseListener
{
View myView;
Map<String, Route> drives;
private TextView driveNumber;
private TextView driveDistance;
private TextView driveTime;
private TextView driveNumList;
private TextView driveDistList;
private TextView driveTimeList;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.weekly_drives, container, false);
APIRequestsUtil.setOnNetWorkListener(this);
return myView;
}
private void populateView() {
this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
drives = APIRequestsUtil.getRoutes();
driveNumber = (TextView) myView.findViewById(R.id.Drive_Number);
driveDistance = (TextView) myView.findViewById(R.id.Drive_Distance);
driveTime = (TextView) myView.findViewById(R.id.Drive_Time);
driveNumber.setText("Drive Num.");
driveDistance.setText("Distance");
driveTime.setText("Time");
int driveNum = 0;
for (Map.Entry drive : drives.entrySet()) {
TableRow tr = new TableRow(getActivity());
Route route = (Route) drive.getValue();
tr.setId(driveNum++);
//tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
DateTime startTime = new DateTime(route.getStart_time());
DateTime endTime = new DateTime(route.getEnd_time());
driveNumList = (TextView) myView.findViewById(R.id.Drive_Number_List);
driveDistList = (TextView) myView.findViewById(R.id.Drive_Distance_List);
driveTimeList = (TextView) myView.findViewById(R.id.Drive_Time_List);
driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator"));
driveDistList.setText(driveDistList.getText().toString() + Float.parseFloat(route.getLen()) / 1000 + " km" + System.getProperty("line.separator"));
driveTimeList.setText(driveTimeList.getText().toString() + ((endTime.getMillis() - startTime.getMillis())/ 1000)/60 + " min" + System.getProperty("line.separator"));
}
}
});
}
@Override
public void onFailure(Request request, Throwable throwable) {
}
@Override
public void onResponse(Response response) {
populateView();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Header aligned to top -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
android:gravity="center"
android:background="#FC9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Number"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Distance"
android:layout_toRightOf="@+id/Drive_Number"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Time"
android:layout_toRightOf="@+id/Drive_Distance"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="#005"
android:layout_below="@+id/header"
android:id="@+id/scrollableContents">
<!--<TableLayout-->
<!--android:id="@+id/fragment1_tlayout"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:stretchColumns="0,1">-->
<!--</TableLayout>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Number_List"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Distance_List"
android:layout_toRightOf="@+id/Drive_Number_List"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Time_List"
android:layout_toRightOf="@+id/Drive_Distance_List"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
输出
drive.entrySet()
答案 0 :(得分:2)
将android:inputType="textMultiLine"
添加到XML中的TextView
。
将数据添加到TextView中:
driveNumList.setText(driveNumList.getText().toString() + String.valueOf(driveNum) + System.getProperty("line.separator"));
等等。
修改:您可以使用"\n"
代替System.getProperty("line.separator")
添加换行符。
答案 1 :(得分:1)
我建议为这种类型的东西使用ListView,以便它可以自动滚动,使用View循环来保持效率,清理代码并将其组织成可消化的段。
要使用ListView,首先需要将其添加到主XML文件中。这将成为以下内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Header aligned to top -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/header"
android:gravity="center"
android:background="#FC9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Number"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Distance"
android:layout_toRightOf="@+id/Drive_Number"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Drive_Time"
android:layout_toRightOf="@+id/Drive_Distance"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#000"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:background="#005"
android:layout_below="@+id/header"
android:id="@+id/scrollableContents">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
然后,您需要创建ListView适配器类。它处理每个个人列表项的数据(你的for循环中的东西)。
以下是您可能会使用的内容,但您可能需要进行自己的调整并导入更多自己的课程,将您的包名称添加到顶部等。
因此,创建一个名为DriveListAdapter.java
的新Java类,并将以下代码放入其中:
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
public class DriveListAdapter extends ArrayAdapter<Map.Entry> {
private final Activity context;
// you may need to change List to something else (whatever is returned from drives.entrySet())
private final List<Map.Entry> drives;
// may also need to change List here (above comment)
public DriveListAdapter(Activity context, List<Map.Entry> drives) {
super(context, R.layout.drive_list_item, drives);
this.context = context;
this.drives = drives;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.drive_list_item, null, true);
Map.Entry drive = this.drives.get(position);
// position is the index of the drives.entrySet() array
int driveNum = position + 1;
// need to import your Route class
Route route = (Route) drive.getValue();
// need to import your DateTime class
DateTime startTime = new DateTime(route.getStart_time());
DateTime endTime = new DateTime(route.getEnd_time());
TextView driveNumList = (TextView) rowView.findViewById(R.id.Drive_Number_List);
TextView driveDistList = (TextView) rowView.findViewById(R.id.Drive_Distance_List);
TextView driveTimeList = (TextView) rowView.findViewById(R.id.Drive_Time_List);
driveNumList.setText(String.valueOf(driveNum));
driveDistList.setText(Double.parseDouble(route.getLen()) / 1000 + " km");
driveTimeList.setText((endTime.getMillis() - startTime.getMillis())/ 1000 + " s");
return rowView;
}
}
然后,创建一个新的布局文件,该文件将是单个列表项的外观。将为列表中的每个驱动器呈现此布局。您可以调整此布局文件,以便正确对齐所有内容。我给出了一个如何做到这一点的例子(创建一个名为drive_list_item.xml
的布局文件):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC"
android:text="Medium Text"
android:id="@+id/Drive_Number_List"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC"
android:text="Medium Text"
android:id="@+id/Drive_Distance_List"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="20sp"
android:textColor="#CCCCCC"
android:text="Medium Text"
android:id="@+id/Drive_Time_List"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
最后,您只需要使用这个新的适配器类填充您在第一步中创建的ListView。
所以使用以下方法(填充ListView而不是使用for循环):
private void populateView() {
this.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
drives = APIRequestsUtil.getRoutes();
driveNumber = (TextView) myView.findViewById(R.id.Drive_Number);
driveDistance = (TextView) myView.findViewById(R.id.Drive_Distance);
driveTime = (TextView) myView.findViewById(R.id.Drive_Time);
driveNumber.setText("Drive Number");
driveDistance.setText("Drive Distance");
driveTime.setText("Drive Time");
// edit: you need to generate your List data from the entrySet
// the ArrayAdapter cannot take a Set argument - needs to be a List
List<Map.Entry> list = new ArrayList<Map.Entry>();
for (Map.Entry drive : drives.entrySet()) {
list.add(drive);
}
// populate the ListView
// may need to change "getActivity()" to something else
// this constructor needs the "this" context of the activity
DriveListAdapter drivesListAdapter = new DriveListAdapter(getActivity(), list);
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(drivesListAdapter);
}
});
}
答案 2 :(得分:0)
首先使用LinearLayout而不是TableLayoyt
<LinearLayout
android:id="@+id/llMainLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
试试这段代码
LinearLayout llMain=(LinearLayout) myView.findViewById(R.id.llMainLayout);
for (Map.Entry drive : drives.entrySet()) {
LinearLayout tr = new LinearLayout(getActivity());
tr.setOrientation(1); // horizontal
Route route = (Route) drive.getValue();
tr.setId(driveNum++);
// set params
DateTime startTime = new DateTime(route.getStart_time());
DateTime endTime = new DateTime(route.getEnd_time());
TextView driveNumList = new TextView();
TextView driveDistList = new TextView();
TextView driveTimeList = new TextView();
driveNumList.setText(String.valueOf(driveNum));
driveDistList.setText(Double.parseDouble(route.getLen()) / 1000 + " km");
driveTimeList.setText((endTime.getMillis() - startTime.getMillis())/ 1000 + " s");
tr.addView(driveNumList);
tr.addView(driveDistList);
tr.addView(driveTimeList);
llMain.addView(tr);
}
}