简体   繁体   English

如何在android中滚动tableview

[英]how to scroll tableview in android

I have an tableview which i want to scroll, because the data is not shown complete. 我有一个我要滚动的tableview,因为数据没有显示完整。

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="0,1,2,3,4"
            android:id="@+id/maintable" >
</TableLayout>

this is my tablelayout, and if i wrap it in an <ScrollView> </Scrollview> the Application crashs if i open the activity. 这是我的tablelayout,如果我将它包装在<ScrollView> </Scrollview> ,如果我打开活动,应用程序就会崩溃。 How to do that? 怎么做?

You should really put the code you tried that crashed your application and the reason for your crash. 你应该把你尝试过的代码丢失在你的应用程序中,以及崩溃的原因。 There's no valuable information in your original post. 您的原始帖子中没有有价值的信息。

Ehhh... did you try something as simple as this? 呃......你尝试过这么简单的事吗? Example .xml that I use a few places: 我使用几个地方的示例.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TableLayout android:id="@+id/score_table"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"> 
        <TableRow android:id="@+id/header"/>                    
    </TableLayout>
</ScrollView>

Works absolutely fine for me. 对我来说绝对没问题。 You don't have to include the TableRow if you have no use for it, obviously. 显然,如果你没有使用它,你不必包括TableRow。

Create your rows dynamically Here I am putting a small example, 动态创建行在这里我举一个小例子,

main.xml main.xml中

<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add row"></Button>

<ScrollView android:id="@+id/ScrollView01" android:layout_width="wrap_content" android:layout_height="wrap_content">

  <TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:stretchColumns="0">

    <TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content" android:layout_height="wrap_content">
      <TextView android:id="@+id/TextView01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="textfield 1-1"></TextView>

      <CheckBox android:id="@+id/CheckBox01" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
    </TableRow>

  </TableLayout>
</ScrollView>

Activity is 活动是

public class tablelayout extends Activity implements OnClickListener {
    /** Called when the activity is first created. */

    //initialize a button and a counter
    Button btn;
    int counter = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // setup the layout
        setContentView(R.layout.main);

        // add a click-listener on the button
        btn = (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(this);        

    }

    // run when the button is clicked
    public void onClick(View view) {

        // get a reference for the TableLayout
        TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);

        // create a new TableRow
        TableRow row = new TableRow(this);

        // count the counter up by one
        counter++;

        // create a new TextView
        TextView t = new TextView(this);
        // set the text to "text xx"
        t.setText("text " + counter);

        // create a CheckBox
        CheckBox c = new CheckBox(this);

        // add the TextView and the CheckBox to the new TableRow
        row.addView(t);
        row.addView(c);

        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    }
}

You can create your table row, when in your code require. 您可以在代码需要时创建表行。 I assumed it is on button click. 我假设它是按下按钮。

Hope this will help. 希望这会有所帮助。

And here one more example . 还有一个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM