2012-04-22 56 views
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> 

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

+0

滑动意味着你在做什么? – 2012-04-22 12:28:50

+0

sliding = scroll – bisemanu 2012-04-22 12:31:38

+0

发布您的xml文件 – 2012-04-22 14:30:12

回答

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

的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了解更多详情

+0

对不起,但我不明白我要做什么来滚动我的代码生成的布局我发布 – bisemanu 2012-04-22 14:22:41

0

这里是它

<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> 
0

尝试这样的事情在你的XML的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> 
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); 
    } 
} 
+0

现在我有这个问题码。插入到tablelayout中的EditText的TableRow中,tablelayout插入到一个Scrollview中,Scrollview插入到Horizo​​ntalScrollView中。 Horizo​​ntalScrollView还包含背景图像。问题是我无法正确滚动并显示所有EditText,因为不是全部都显示。你可以帮我吗? – bisemanu 2012-04-26 12:34:14

0

有点晚了,但你可以看看这个图书馆我从蝙蝠侠的水平和垂直滚动浏览here