我正在尝试使用冻结标头创建一个表。我可以通过一个tablelayout实现这一点,该tablelayout包含一个包含另一个tablelayout的scrollview,但我的标题对齐已经结束。
继承XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/table_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
</TableLayout>
</ScrollView>
</TableLayout>
并且代码
public class PrepositionRulesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rules);
init();
}
public void init(){
createTableHeader();
createTableBody();
// alignRows();
}
private Collection<Preposition> load(Context context) {
DataSource<Preposition> dataSource = new DataSourceFactory().createPrepositionDataSource(context);
return dataSource.get();
}
private void createTableHeader() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_header);
tableLayout.addView(createHeaderRow(), 0);
}
private void createTableBody() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_body);
int row = 0;
TableRow tableRow = createHeaderRow();
tableLayout.addView(tableRow, row++);
for (Preposition preposition : load(this)) {
tableLayout.addView(createBodyRow(preposition), row++);
}
}
private TableRow createHeaderRow() {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(getString(R.string.preposition)));
row.addView(createCell(Kasus.Akkusativ.name()));
row.addView(createCell(Kasus.Dativ.name()));
row.addView(createCell(Kasus.Genitiv.name()));
return row;
}
private TableRow createBodyRow(Preposition preposition) {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(preposition.getPreposition()));
row.addView(createCell(preposition.contains(Kasus.Akkusativ)));
row.addView(createCell(preposition.contains(Kasus.Dativ)));
row.addView(createCell(preposition.contains(Kasus.Genitiv)));
return row;
}
private void alignRows() {
TableLayout headerLayout = (TableLayout) findViewById(R.id.table_header);
TableRow headerRow = (TableRow)headerLayout.getChildAt(0);
TableLayout bodyLayout = (TableLayout) findViewById(R.id.table_body);
TableRow bodyRow = (TableRow)bodyLayout.getChildAt(0);
headerLayout.setLayoutParams(bodyLayout.getLayoutParams());
for(int i = 0; i < bodyRow.getChildCount(); i++) {
headerRow.getChildAt(i).setLayoutParams(bodyRow.getChildAt(i).getLayoutParams());
}
}
private View createCell(boolean isEnabled) {
if (isEnabled) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.ic_done_black_24dp);
return view;
}
return new Space(this);
}
private View createCell(String text) {
TextView view = new TextView(this);
view.setPadding(15, 0, 15, 5);
view.setTypeface(Typeface.DEFAULT_BOLD);
view.setText(text);
return view;
}
}
我以为我可以通过向主体表添加标题行的副本来实现对齐,而不是将其布局复制到添加到标题表的行但是不起作用。
任何建议都将不胜感激
图片显示错位:
答案 0 :(得分:0)
很难控制tablelayout中的对齐方式,因为列的宽度总是取决于并且会受到同一个表中另一行的另一列宽度的影响。
为了创建一个整齐对齐的表,我建议你使用LinearLayout(用于标题和行)和listview来显示项目(行)。
表的标题行和列表行的示例row.xml:
<LinearLayout
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="beginning|middle|end">
<TextView
android:id="@+id/model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="4dp"
android:text="@string/model"
android:textStyle="bold"/>
<TextView
android:id="@+id/color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/color"
android:textStyle="bold"/>
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="@string/status"
android:textStyle="bold"/>
</LinearLayout>
这是您在activity_main.xml中应用它的方式:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<include layout="@layout/row"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
最后,为listview的适配器中的每个列表项扩展相同的row.xml。下面是上面的xmls创建的表格的屏幕截图。请注意,在显示表格中的每个行项目时,我已删除了分隔符并以编程方式将textstyle设置为正常。