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

カスタム垂直 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>

詳細については、このリンクを参照してください

于 2012-04-22T12:36:15.920 に答える
0

少し遅れましたが、Android の Horizo​​ntal および Vertical scrollviews からまとめたこのライブラリをここでチェックできます。

于 2015-03-02T19:50:43.557 に答える
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

私はこのように解決しました:

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で次のようなことを試してください:

<?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 に答える