简体   繁体   English

动态创建Android的水平和垂直滚动视图

[英]Create Horizontal and Vertical Scroll View Dynamically Android

In my project i managed to display the database in the form of rows and columns of edit Texts and text Views.But the as the number of rows and columns increase it is not possible to view the added data , they dont fit into the screen. 在我的项目中,我设法以编辑文本和文本视图的行和列的形式显示数据库。但是随着行和列数的增加,无法查看添加的数据,它们不适合屏幕。 so i think i needed a horizontal scrollview and a vertical scrollview for viewing entire rows and columns. 所以我想我需要一个水平滚动视图和一个垂直滚动视图来查看整个行和列。 Below is my code kindly help me how to achieve this. 以下是我的代码,请帮助我如何实现此目标。 Please do help me thanks in advance 请事先帮助我

xml code is xml代码是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">



<TableLayout 
            android:layout_width="wrap_content"
            android:stretchColumns="0,1,2,3,4,5"
            android:id="@+id/maintable"
            android:layout_height="wrap_content"  
            android:layout_weight="1.0">

</TableLayout>
</LinearLayout>

The following is the code for page where i wanna display my rows and columns 以下是我要显示行和列的页面的代码

public class ViewActivity extends Activity {
EditText col1;
EditText col2;
TextView Id;
EditText col4;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view);
    ScrollView sv = new ScrollView(this);
    HorizontalScrollView hs =new HorizontalScrollView(this);

    TableLayout tl = (TableLayout) findViewById(R.id.maintable);

    //CREATING A LIST OF HEADINGS

TableRow tr1 = new TableRow(this);
    tr1.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ));

    Id = new TextView(this);
    Id.setText("COL1");
    tr1.addView(Id);

    Id = new TextView(this);
    Id.setText("COL2");
    tr1.addView(Id);

    Id = new TextView(this);
    Id.setText("COL3");
    tr1.addView(Id);

    Id = new TextView(this);
    Id.setText("Rssi");
    tr1.addView(COL4);

    Id = new TextView(this);
    Id.setText("Delete");
    tr1.addView(COL5);

    Id = new TextView(this);
    Id.setText("Update");
    tr1.addView(COL6);

    hs.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.FILL_PARENT));



    tl.addView(tr1,new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));  

    // CREATING ROWS AND COLUMNS DYNAMICALLY


    final DBclass entry = new DBclass(ViewActivity.this);
    entry.open();
    Cursor c = entry.getAllRecords();

    int count =0;

    if(c.moveToFirst()){
        do{
            TableRow tr = new TableRow(this);
            tr.setId(count);
            tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ));

            /* CODE FOR
              COLUMNS DISPLAYED IN THE FORM OF 
              EDITTEXTS AND TEXTVIEWS
            */

            tl.addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));



            count++;
        }while(c.moveToNext());

    }

}

I'm fairly sure you could simply put your TableLayout inside a ScrollView to solve this problem. 我相当确定您可以将TableLayout放在ScrollView中以解决此问题。 No need to fiddle with adding Views programatically, just predefine it in the XML layout resource. 无需费力地以编程方式添加View,只需在XML布局资源中预定义即可。 Like this: 像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <TableLayout 
            android:layout_width="wrap_content"
            android:stretchColumns="0,1,2,3,4,5"
            android:id="@+id/maintable"
            android:layout_height="wrap_content"  
            android:layout_weight="1.0">

    </TableLayout>

</ScrollView>

</LinearLayout>

Keep in mind ScrollView is vertical, so you'd need to add a HorizontalScrollView in there to add horizontal scrolling as well. 请记住,ScrollView是垂直的,因此您需要在其中添加Horizo​​ntalScrollView来添加水平滚动。

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

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