Android表格与冻结标题对齐整齐

Android表格与冻结标题对齐整齐,android,android-layout,android-tablelayout,Android,Android Layout,Android Tablelayout,我正在尝试创建一个标题冻结的表。我可以通过拥有一个包含一个滚动视图的tablelayout来实现这一点,该滚动视图包装了另一个tablelayout,但我的标题对齐不正确 这里是XML <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/table_header"

我正在尝试创建一个标题冻结的表。我可以通过拥有一个包含一个滚动视图的tablelayout来实现这一点,该滚动视图包装了另一个tablelayout,但我的标题对齐不正确

这里是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;
    }
}
公共类PrepositionRulesActivity扩展了AppCompativeActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_规则);
init();
}
公共void init(){
createTableHeader();
createTableBody();
//对齐行();
}
私有集合加载(上下文){
DataSource DataSource=new DataSourceFactory().createPrepositionDataSource(上下文);
返回dataSource.get();
}
私有void createTableHeader(){
TableLayout TableLayout=(TableLayout)findViewById(R.id.table_头);
tableLayout.addView(createHeaderRow(),0);
}
私有void createTableBody(){
TableLayout TableLayout=(TableLayout)findViewById(R.id.table_body);
int行=0;
TableRow TableRow=createHeaderRow();
tableLayout.addView(tableRow,row++);
for(介词介词:加载(此)){
tableLayout.addView(createBodyRow(介词),row++);
}
}
私有表行createHeaderRow(){
TableRow row=新的TableRow(本);
TableRow.LayoutParams rowLayout=新建TableRow.LayoutParams(TableRow.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
rowLayout.gravity=gravity.CENTER\u水平;
row.setLayoutParams(rowLayout);
addView(createCell(getString(R.string.preposition));
addView(createCell(Kasus.Akkusativ.name());
row.addView(createCell(Kasus.Dativ.name());
addView(createCell(Kasus.Genitiv.name());
返回行;
}
private TableRow createBodyRow(介词介词){
TableRow row=新的TableRow(本);
TableRow.LayoutParams rowLayout=新建TableRow.LayoutParams(TableRow.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
rowLayout.gravity=gravity.CENTER\u水平;
row.setLayoutParams(rowLayout);
addView(createCell(preposition.getPreposition());
addView(createCell(介词.contains(Kasus.Akkusativ));
addView(createCell(介词.contains(Kasus.Dativ));
addView(createCell(介词.contains(Kasus.Genitiv));
返回行;
}
私有void行(){
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());
对于(int i=0;i
我认为我可以通过将标题行的副本添加到body表中,而不是将其布局复制到添加到标题表中的行中来实现对齐,但这不起作用

如有任何建议,将不胜感激

显示未对准的图像:


很难控制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。下面是由上述XML创建的表的屏幕截图。请注意,在显示表中的每一行项目时,我已经删除了分隔符,并以编程方式将textstyle设置为normal


很难控制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。下面是由上述XML创建的表的屏幕截图。请注意,在显示表中的每一行项目时,我已经删除了分隔符,并以编程方式将textstyle设置为normal