以编程方式将 TableRows 添加到 TableLayout 不显示
Programmatically added TableRows to a TableLayout don't show
我是 Android 的新手。我试图将 TableRows
添加到 TableLayout
但它不起作用。我不知道为什么它不显示。我试图从互联网上看,但似乎我的问题不一样。我在 logcat 中没有异常或错误。我尝试调试并发现添加了视图,但由于某种原因它们没有显示。有人对此有想法吗?提前致谢。
我的代码:
private void addInfosToTable(){ //this is called in onCreate() function
//these are just dummy infos i want to test the how the table works
Cell cell = new Cell("1234",5000,3000);
cell.setStatus(Cell.Status.COMPLETE);
Cell cell1 = new Cell("1234",5000,3000);
cell1.setStatus(Cell.Status.FAILED);
addCellRowToTable(cell);
addCellRowToTable(cell1);
}
private void addCellRowToTable(Cell cell){
//init cell info
String cellID = cell.getCellID();
double targetSpeed = cell.targetSpeed();
double targetPoint = cell.targetPoint();
Cell.Status status = cell.getStatus();
//create tableRow
TableRow tableRow = new TableRow(this);
tableRow.addView(getTextView(cellID,true));
tableRow.addView(getTextView("n.a"+"/ "+"n.a",false));
tableRow.addView(getTextView(targetSpeed+"/ "+targetPoint,false));
tableRow.addView(getStatusView(status));
tableRow.setPadding(0,R.dimen.table_marginVertical,R.dimen.table_marginVertical,0);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
//add tableRow to tableLayout
this.cellTable.addView(tableRow);
}
private TextView getTextView(String text, boolean isFirst){
TextView textView = new TextView(this);
textView.setTextSize(R.dimen.table_textSize);
textView.setText(text);
textView.setTextColor(ContextCompat.getColor(this,R.color.black));
if (isFirst){
textView.setPadding(R.dimen.table_marginVertical,0,0,0);
}
return textView;
}
private ImageView getStatusView(Cell.Status status){
ImageView imageView = new ImageView(this);
switch (status){
case COMPLETE:
imageView.setImageResource(R.drawable.success);
return imageView;
default:
imageView.setImageResource(R.drawable.failed);
return imageView;
}
}
我的 table 有 4 列,看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="superapp.networkapp.MainActivity"
android:focusableInTouchMode="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/warning_box"
android:padding="10dp"
android:layout_margin="3dp"
android:weightSum="1">
<TextView
android:layout_width="276dp"
android:layout_height="wrap_content"
android:text="@string/warning_text"
android:id="@+id/main_boxText"
android:layout_margin="3dp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_weight="0.49"
android:textSize="12sp"
android:textIsSelectable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/main_boxIcon"
android:src="@drawable/warning_icon"
android:layout_weight="0.47" />
</LinearLayout>
<TableLayout
android:stretchColumns = "*"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cellListTable">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:paddingTop="@dimen/table_marginVertical"
android:paddingBottom="@dimen/table_marginVertical"
android:id="@+id/cellTableHeading">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/cells"
android:id="@+id/cell"
android:layout_column="0"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true"
android:paddingLeft="@dimen/table_marginVertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/table_current_info"
android:id="@+id/currentInfo"
android:layout_column="1"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/table_target_info"
android:id="@+id/targetInfo"
android:layout_column="2"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/status"
android:id="@+id/status"
android:layout_column="3"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true"
android:textAlignment="textEnd"
android:paddingRight="@dimen/table_marginVertical" />
</TableRow>
</TableLayout>
</LinearLayout>
我不知道为什么它不显示。我试图从互联网上看,但似乎我的问题不一样。我在 logcat 中没有异常或错误。我尝试调试并发现添加了视图,但由于某种原因它们没有显示。有人对此有想法吗?提前致谢。
更新
我将默认视图的布局高度编辑为全部wrap_content
。我还添加了这行代码:在将 table 行添加到 tableLayout
之前。但是还是不行。
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
您必须为每个 table 行定义一个布局,并将 textView 和 ImageView 绑定到布局中的相应元素。
我是 Android 的新手。我试图将 TableRows
添加到 TableLayout
但它不起作用。我不知道为什么它不显示。我试图从互联网上看,但似乎我的问题不一样。我在 logcat 中没有异常或错误。我尝试调试并发现添加了视图,但由于某种原因它们没有显示。有人对此有想法吗?提前致谢。
我的代码:
private void addInfosToTable(){ //this is called in onCreate() function
//these are just dummy infos i want to test the how the table works
Cell cell = new Cell("1234",5000,3000);
cell.setStatus(Cell.Status.COMPLETE);
Cell cell1 = new Cell("1234",5000,3000);
cell1.setStatus(Cell.Status.FAILED);
addCellRowToTable(cell);
addCellRowToTable(cell1);
}
private void addCellRowToTable(Cell cell){
//init cell info
String cellID = cell.getCellID();
double targetSpeed = cell.targetSpeed();
double targetPoint = cell.targetPoint();
Cell.Status status = cell.getStatus();
//create tableRow
TableRow tableRow = new TableRow(this);
tableRow.addView(getTextView(cellID,true));
tableRow.addView(getTextView("n.a"+"/ "+"n.a",false));
tableRow.addView(getTextView(targetSpeed+"/ "+targetPoint,false));
tableRow.addView(getStatusView(status));
tableRow.setPadding(0,R.dimen.table_marginVertical,R.dimen.table_marginVertical,0);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
//add tableRow to tableLayout
this.cellTable.addView(tableRow);
}
private TextView getTextView(String text, boolean isFirst){
TextView textView = new TextView(this);
textView.setTextSize(R.dimen.table_textSize);
textView.setText(text);
textView.setTextColor(ContextCompat.getColor(this,R.color.black));
if (isFirst){
textView.setPadding(R.dimen.table_marginVertical,0,0,0);
}
return textView;
}
private ImageView getStatusView(Cell.Status status){
ImageView imageView = new ImageView(this);
switch (status){
case COMPLETE:
imageView.setImageResource(R.drawable.success);
return imageView;
default:
imageView.setImageResource(R.drawable.failed);
return imageView;
}
}
我的 table 有 4 列,看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="superapp.networkapp.MainActivity"
android:focusableInTouchMode="true">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.google.android.gms.maps.MapFragment"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/warning_box"
android:padding="10dp"
android:layout_margin="3dp"
android:weightSum="1">
<TextView
android:layout_width="276dp"
android:layout_height="wrap_content"
android:text="@string/warning_text"
android:id="@+id/main_boxText"
android:layout_margin="3dp"
android:textColor="@color/black"
android:textStyle="bold"
android:layout_weight="0.49"
android:textSize="12sp"
android:textIsSelectable="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/main_boxIcon"
android:src="@drawable/warning_icon"
android:layout_weight="0.47" />
</LinearLayout>
<TableLayout
android:stretchColumns = "*"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/cellListTable">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:paddingTop="@dimen/table_marginVertical"
android:paddingBottom="@dimen/table_marginVertical"
android:id="@+id/cellTableHeading">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/cells"
android:id="@+id/cell"
android:layout_column="0"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true"
android:paddingLeft="@dimen/table_marginVertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/table_current_info"
android:id="@+id/currentInfo"
android:layout_column="1"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/table_target_info"
android:id="@+id/targetInfo"
android:layout_column="2"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/status"
android:id="@+id/status"
android:layout_column="3"
android:textColor="@color/white"
android:textColorHighlight="@color/blue"
android:textSize="@dimen/table_textSize"
android:textIsSelectable="true"
android:textAlignment="textEnd"
android:paddingRight="@dimen/table_marginVertical" />
</TableRow>
</TableLayout>
</LinearLayout>
我不知道为什么它不显示。我试图从互联网上看,但似乎我的问题不一样。我在 logcat 中没有异常或错误。我尝试调试并发现添加了视图,但由于某种原因它们没有显示。有人对此有想法吗?提前致谢。
更新
我将默认视图的布局高度编辑为全部wrap_content
。我还添加了这行代码:在将 table 行添加到 tableLayout
之前。但是还是不行。
tableRow.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.MATCH_PARENT));
您必须为每个 table 行定义一个布局,并将 textView 和 ImageView 绑定到布局中的相应元素。