如何使用HorzontalScroll View以编程方式创建列

时间:2017-03-23 22:59:33

标签: android android-tablelayout

您好我一直在寻找答案,但由于某种原因,我找到的大部分答案都没有帮助我。我在我的片段上创建了一个tablelayout,我正在以编程方式添加一个带有文本视图的简单tablerow。目前有5个或更多的列似乎被打包到左角有没有任何方法可以拉伸到视图?谢谢你的帮助!

更新:删除水平视图可以解决问题,但有没有办法让表格拉伸水平滚动?或者我是否必须手动为每个单元格添加宽度?

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#3d455b">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true" >

    <HorizontalScrollView
        android:id="@+id/hscrll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/RelativeLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/entries"
        android:stretchColumns="*">

        <TableRow android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/row_cell">
        </TableRow>

    </TableLayout>
        </RelativeLayout>
    </HorizontalScrollView>
    </ScrollView>
</RelativeLayout>

JAVA:

 valuesTable.removeAllViews();

    //add header
            TableRow tbrow0 = new TableRow(ctx);
            tbrow0.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
            TextView tv0 = new TextView(ctx);
            tv0.setText("Id");
            tv0.setTextSize(20);
            tv0.setTextColor(Color.WHITE);
            tv0.setPadding(10, 10, 10, 10);
            tbrow0.addView(tv0);
TextView tv0 = new TextView(ctx);
            tv0.setText("name");
            tv0.setTextSize(20);
            tv0.setTextColor(Color.WHITE);
            tv0.setPadding(10, 10, 10, 10);
            tbrow0.addView(tv0);
TextView tv0 = new TextView(ctx);
            tv0.setText("color");
            tv0.setTextSize(20);
            tv0.setTextColor(Color.WHITE);
            tv0.setPadding(10, 10, 10, 10);
            tbrow0.addView(tv0);


            valuesTable.addView(tbrow0);

            //Add row information

                TableRow tbrow = new TableRow(ctx);
                tbrow.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                for(String value : row.getValues()){
                    TextView t1v = new TextView(ctx);               
                    t1v.setText(value);
                    t1v.setTextSize(17);
                    t1v.setTextColor(getResources().getColor(R.color.darkgray));
                    t1v.setPadding(10, 10, 10, 10);
                    t1v.setGravity(Gravity.CENTER);
                    tbrow.addView(t1v);
                }

                valuesTable.addView(tbrow);

0 个答案:

没有答案