膨胀 tableRow 时无法到达 TextView 参数
Can't reach TextView parameters while inflating tableRow
我是 Android 开发新手。
目前我正在尝试用我的数据填充 TableLayout。
数据部分工作得很好,但来自 TableRow 的 TextView 参数不会影响我的设计。
这是我的 Activity 与 table:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<include layout = "@layout/table_row"/>
</TableLayout>
</ScrollView>
这里是 TableRow XML 我用来膨胀
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBFBFB">
<TextView
android:id="@+id/number"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="2dp"
android:layout_weight="0"
android:textSize="16sp"/>
<TextView
android:id="@+id/school_name"
android:textSize="16sp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:text="School Name"/>
<TextView
android:id="@+id/rating"
android:textSize="16sp"
android:padding="2dp"
android:layout_weight="2"
android:text="Rating"/>
</TableRow>
和Java代码:
public void fillTable () {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
LayoutInflater inflater = LayoutInflater.from(this);
for(int i = 0; i<schools.size(); i++){
addRow(i, inflater, tableLayout);
}
}
public void addRow(int num, LayoutInflater inflater, TableLayout parent) {
TableRow tr = (TableRow) inflater.inflate(R.layout.table_row,parent,false);
tr.setId(num + 1);
TextView tv = tr.findViewById(R.id.number);
tv.setText(String.valueOf(num+1));
tv = tr.findViewById(R.id.school_name);
tv.setText(schools.get(num).schoolName);
tv = tr.findViewById(R.id.rating);
tv.setText(String.valueOf(schools.get(num).score));;
final int index = num ;
tr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SchoolCard.class);
intent.putExtra(SCHOOL_LINK,schools.get(index).schoolCardLink);
startActivity(intent);
}
});
parent.addView(tr);
}
找不到问题所在。
将 Table 行放在 Table 选项卡中,然后 运行。
我是 Android 开发新手。
目前我正在尝试用我的数据填充 TableLayout。
数据部分工作得很好,但来自 TableRow 的 TextView 参数不会影响我的设计。
这是我的 Activity 与 table:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
<include layout = "@layout/table_row"/>
</TableLayout>
</ScrollView>
这里是 TableRow XML 我用来膨胀
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBFBFB">
<TextView
android:id="@+id/number"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="2dp"
android:layout_weight="0"
android:textSize="16sp"/>
<TextView
android:id="@+id/school_name"
android:textSize="16sp"
android:background="@color/colorAccent"
android:layout_weight="1"
android:text="School Name"/>
<TextView
android:id="@+id/rating"
android:textSize="16sp"
android:padding="2dp"
android:layout_weight="2"
android:text="Rating"/>
</TableRow>
和Java代码:
public void fillTable () {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
LayoutInflater inflater = LayoutInflater.from(this);
for(int i = 0; i<schools.size(); i++){
addRow(i, inflater, tableLayout);
}
}
public void addRow(int num, LayoutInflater inflater, TableLayout parent) {
TableRow tr = (TableRow) inflater.inflate(R.layout.table_row,parent,false);
tr.setId(num + 1);
TextView tv = tr.findViewById(R.id.number);
tv.setText(String.valueOf(num+1));
tv = tr.findViewById(R.id.school_name);
tv.setText(schools.get(num).schoolName);
tv = tr.findViewById(R.id.rating);
tv.setText(String.valueOf(schools.get(num).score));;
final int index = num ;
tr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SchoolCard.class);
intent.putExtra(SCHOOL_LINK,schools.get(index).schoolCardLink);
startActivity(intent);
}
});
parent.addView(tr);
}
找不到问题所在。
将 Table 行放在 Table 选项卡中,然后 运行。