【问题标题】:HorizontalScrollView not working inside table layoutHorizo​​ntalScrollView 在表格布局中不起作用
【发布时间】:2014-10-17 02:03:48
【问题描述】:

我无法向右滚动,因为我的列是动态绘制的。我完全不知道。我尝试使用horizo​​ntalscrollview,但仍然无法向右滚动。任何解决方案将不胜感激。

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>

代码。

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));
        }

【问题讨论】:

  • 我之前也遇到过类似的问题。尝试调整滚动视图的宽度和表格布局。
  • @hoomi 我试过了,但还是没有成功。
  • 你可以试试LinearLayout(水平方向)而不是TableLayout
  • @kaushik 您的意思是在 holizontalscrollview 下方但在表格布局上方添加线性布局?

标签: android scrollview tablelayout


【解决方案1】:

我稍微修改了您的视图,它在我的 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>

只是对您的布局做一点说明。尽量避免深度视图层次结构,以避免性能问题并更容易调试:)

【讨论】:

  • 你修改了什么?我试过模拟器还是不行?
  • 我更改了重复的 id,还使您的标题文本更长。我的应用程序只是您的布局的一个简单活动。我没有添加您拥有的 java 代码。我建议尝试类似的方法。先把 UI 弄好,然后开始添加 java 代码
  • 当我评论这个 //tl1.setLayoutParams(params1);在 tl1 中,它设法滚动,但布局全部运行。
  • 尝试用new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 5f)替换所有new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 5f);
  • 所有布局仍在运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
相关资源
最近更新 更多