如何在动态创建的水平和垂直布局中滚动?

时间:2012-04-22 12:28:06

标签: android android-layout android-widget

我想了解如何在水平和垂直方向上制作动态创建的布局。

这是创建布局的代码片段:

tableLayout = new TableLayout(this);
tableLayout.setGravity(Gravity.CENTER);

values = new EditText[10][10];

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

tableRow = new TableRow(this); 
tableRow.setGravity(Gravity.CENTER);

for (int j = 0; j < 10  ; j++) {
values[i][j] = new EditText(this);
values[i][j].setHint("r " + i + "c" +j);
values[i][j].setPadding(10, 10, 10, 10);
tableRow.addView(values[i][j]);
}

tableLayout.addView(tableRow);
}

setContentView(tableLayout);

XML文件:

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


</LinearLayout>

我想滚动它。我希望你能提供帮助 感谢

5 个答案:

答案 0 :(得分:3)

自定义垂直ScrollView:

package com.scrollable.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class VScroll extends ScrollView {

    public VScroll(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public VScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public VScroll(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

自定义Horizo​​ntalScrollView:

package com.scrollable.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;

public class HScroll extends HorizontalScrollView {

    public HScroll(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public HScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HScroll(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

ScrollableImageActivity:

package com.scrollable.view;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

public class ScrollableImageActivity extends Activity {

    private float mx, my;
    private float curX, curY;

    private ScrollView vScroll;
    private HorizontalScrollView hScroll;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        vScroll = (ScrollView) findViewById(R.id.vScroll);
        hScroll = (HorizontalScrollView) findViewById(R.id.hScroll);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float curX, curY;

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                mx = event.getX();
                my = event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                curX = event.getX();
                curY = event.getY();
                vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                mx = curX;
                my = curY;
                break;
            case MotionEvent.ACTION_UP:
                curX = event.getX();
                curY = event.getY();
                vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                hScroll.scrollBy((int) (mx - curX), (int) (my - curY));
                break;
        }

        return true;
    }

}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.scrollable.view.VScroll android:layout_height="fill_parent"
        android:layout_width="fill_parent" android:id="@+id/vScroll">
        <com.scrollable.view.HScroll android:id="@+id/hScroll"
            android:layout_width="fill_parent" android:layout_height="fill_parent">
            <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/bg"></ImageView>
        </com.scrollable.view.HScroll>
    </com.scrollable.view.VScroll>

</LinearLayout>

有关详细信息,请参阅此LINK

答案 1 :(得分:0)

这是xml for it

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:scrollbars="vertical">

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


        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linlay" android:layout_width="320px"
            android:layout_height="fill_parent" android:stretchColumns="1"
            android:background="#000000"/>

    </HorizontalScrollView>

</ScrollView>

答案 2 :(得分:0)

在xml中尝试这样的事情:

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

    <ScrollView 
        android:scrollbars="horizontal" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent">
        <HorizontalScrollView 
            android:id="@+id/horizontalView" 
            android:layout_height="wrap_content"
            android:scrollbars="horizontal|vertical" 
            android:layout_width="wrap_content">

              //put here your child layout that you want to scroll(don't forget that the scrollview should have just one child)

       </HorizontalScrollView>

    </ScrollView>

</LinearLayout>

答案 3 :(得分:0)

我这样解决了:

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.Gravity;

public class TableActivity extends Activity 
{ 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        HorizontalScrollView HSC = new HorizontalScrollView(this);
        ScrollView VSC = new ScrollView(this);
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setGravity(Gravity.CENTER);
        EditText[][] values = new EditText[10][10];
        for (int i = 0; i < 15; i++) 
        { 
            TableRow tableRow = new TableRow(this);
            tableRow.setGravity(Gravity.CENTER);
            for (int j = 0; j < 15; j++) 
            { 
                values[i][j] = new EditText(this);
                values[i][j].setHint("r " + i + "c" +j);
                values[i][j].setPadding(10, 10, 10, 10);
                tableRow.addView(values[i][j]);
            }
            tableLayout.addView(tableRow);
        }
        VSC.addView(tableLayout);
        HSC.addView(VSC);
        setContentView(HSC);
    }
}

答案 4 :(得分:0)

有点晚了,但是你可以看看这个我从头颅的水平和垂直滚动视图here&#