2012-04-22 77 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