0

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

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

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>

我想滚动它。我希望你能帮忙谢谢

4

5 回答 5

3

自定义垂直滚动视图:

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

可滚动图像活动:

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>

有关更多详细信息,请参阅此链接

于 2012-04-22T12:36:15.920 回答
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);
    }
}
于 2012-04-22T20:30:32.643 回答
0

这是它的xml

<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>
于 2012-04-22T12:37:26.513 回答
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>
于 2012-04-22T12:39:28.713 回答
0

有点晚了,但你可以在这里查看我从 android 的水平和垂直滚动视图中组合在一起的这个库

于 2015-03-02T19:50:43.557 回答