HorizontalScrollView not working inside table layout

ぃ、小莉子 提交于 2019-12-12 03:48:39

问题


I'am unable to scroll to the right since my column is dynamically draw. I'm totally out of idea. I tried use horizontalscrollview but still unable to scroll to the right. Any solutions will appreciated.

xml

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:fillViewport="true"
    android:layout_height="fill_parent"> 

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tl"
        android:weightSum="5"
        >

      <TableRow>
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1"
                android:text="test" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"

                android:layout_weight="1"

                android:text="test" />
          <TableLayout android:stretchColumns="*"
              android:layout_weight="3">
              <TableRow>
                    <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"

                android:text="test" />

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"

                android:text="test" />

              </TableRow>

          </TableLayout>

      </TableRow>



    </TableLayout>
 </HorizontalScrollView>

code.

TableLayout tl, tl5;

tl = (TableLayout) findViewById(R.id.tl);
tl.removeAllViews();
TableRow tr3 = new TableRow(this);

TextView lblrecID11 = new TextView(this);
lblrecID11.setBackgroundResource(R.drawable.cell_shape);
lblrecID11.setText("column1");

lblrecID11.setGravity(Gravity.CENTER); 
LayoutParams params44 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.3f);
lblrecID11.setLayoutParams(params44);
tr3.addView(lblrecID11);

TextView lblrecName11= new TextView(this);
lblrecName11.setBackgroundResource(R.drawable.cell_shape);
lblrecName11.setText("column2");
lblrecName11.setGravity(Gravity.CENTER);
LayoutParams params3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
lblrecName11.setLayoutParams(params3);
tr3.addView(lblrecName11); 

TextView lblrecName12= new TextView(this);
lblrecName12.setBackgroundResource(R.drawable.cell_shape);
lblrecName12.setText("column3");
lblrecName12.setGravity(Gravity.CENTER);
lblrecName12.setLayoutParams(params3);
tr3.addView(lblrecName12); 

tl5 = new TableLayout(this);
tl5.setStretchAllColumns(true);
LayoutParams params4 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 5f);
tl5.setLayoutParams(params4);
tr3.addView(tl5);

TableRow tr4 = new TableRow(this);
TextView lblrecID5 = new TextView(this);
lblrecID5.setText("Column Top 4");
lblrecID5.setBackgroundResource(R.drawable.cell_shape);
lblrecID5.setGravity(Gravity.CENTER); 
TableRow.LayoutParams rowParams2 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
rowParams2.span = 30;
lblrecID5.setLayoutParams(rowParams2);
tr4.addView(lblrecID5);// add the column to the table row here

TableRow tr5 = new TableRow(this);
for(int i=0;i<30;i++)
{
TextView lblrecID6 = new TextView(this);
lblrecID6.setText("test1010");
lblrecID6.setBackgroundResource(R.drawable.cell_shape);
lblrecID6.setGravity(Gravity.CENTER); 
lblrecID6.measure(0, 0);  
int width = lblrecID6.getMeasuredWidth(); 
myListWidth.add(width);
tr5.addView(lblrecID6);
}

tl5.addView(tr4);
tl5.addView(tr5);

tl.addView(tr3, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

for(int i = 0;i<5;i++)
{

    TableRow tr = new TableRow(this);

    TextView lblrecID = new TextView(this);
    lblrecID.setBackgroundResource(R.drawable.cell_shape);
    lblrecID.setText("testrow");
    lblrecID.setSingleLine(true);
    lblrecID.setGravity(Gravity.CENTER); 
    LayoutParams params22 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.3f);
    lblrecID.setLayoutParams(params22);
    tr.addView(lblrecID);

    TextView lblrecName = new TextView(this);
    lblrecName.setId(200+i);
    lblrecName.setBackgroundResource(R.drawable.cell_shape);
    lblrecName.setText("test123");

    lblrecName.setGravity(Gravity.CENTER);
    LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
    lblrecName.setLayoutParams(params);
    tr.addView(lblrecName);

    TextView lblrecName3 = new TextView(this);
    lblrecName3.setBackgroundResource(R.drawable.cell_shape);
    lblrecName3.setText(" ");
    lblrecName3.setGravity(Gravity.CENTER);
    lblrecName3.setLayoutParams(params);
    tr.addView(lblrecName3); 

    TableLayout tl1 = new TableLayout(this);
    LayoutParams params1 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 5f);
    tl1.setStretchAllColumns(true);
    tl1.setLayoutParams(params1);
    TableRow tr2 = new TableRow(this);

    for(int j=0;j<30;j++)
    {
        TextView lblrecID1 = new TextView(this);
        lblrecID1.setBackgroundResource(R.drawable.cell_shape);
        lblrecID1.setText("test1010");
        lblrecID1.setGravity(Gravity.CENTER); 

        tr2.addView(lblrecID1);
    }

    tl1.addView(tr2);     
    tr.addView(tl1);
    tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        }

回答1:


I have modified your view a little bit and it was scrolling on my HTC One.

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:fillViewport="true"
    android:layout_height="fill_parent">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tl"
        android:weightSum="5"
        >

        <TableRow>
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_weight="1"
                android:text="testieowhif iefh wf wihfw ifhw ef" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"

                android:layout_weight="1"

                android:text="test ihfi3 fi fihi f " />
            <TableLayout android:stretchColumns="*"
                android:layout_weight="3">
                <TableRow>
                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"

                        android:text="test 3ihr 3iru3h 3i 3ihr3 ri3h 3irh3 " />

                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"

                        android:text="test iuhr i3ihr 3iuh3irhi 3ruhi" />

                </TableRow>

            </TableLayout>

        </TableRow>



    </TableLayout>
</HorizontalScrollView>

Just a little note on your layout. try to avoid having deep view hierarchies as much as you can to avoid performance issues and also easier debug :)



来源:https://stackoverflow.com/questions/25460200/horizontalscrollview-not-working-inside-table-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!