2011-09-19 149 views
0

我正在使用android,我想在運行時進行水平滾動視圖,意味着我想通過java代碼製作這個水平視圖。Android:如何使水平滾動視圖?

這是我的java類的代碼

package com.pericent; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TabHost; 

public class HelloTabWidget extends TabActivity { 

    private String TAG="HelloTabWidget"; 

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

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 

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

     // Create an Intent to launch an Activity for the tab (to be reused) 
       intent = new Intent().setClass(this,ArtistsActivity.class); 
       Log.v(TAG,"---artist activity is called---"); 
     // Initialize a TabSpec for each tab and add it to the TabHost 
       spec = tabHost.newTabSpec("artists").setIndicator("Artists",res.getDrawable(R.drawable.ic_tab_artists)).setContent(intent); 
       tabHost.addTab(spec); 
     } 
     setContentView(tabHost); 
    } 
} 


this is activity ArtistActivity which is used in above code to make Tab Widget:- 
package com.pericent; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

public class ArtistsActivity extends Activity { 
    private String TAG="ArtistsActivity"; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView textview=new TextView(this); 
     textview.setText("This is Artist Activity"); 
     setContentView(textview); 
     Log.v(TAG,"---in artist activity---"); 
    } 
} 

and this is the xml file used in above code:- 
<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/ic_tab_artists_grey" 
      android:state_selected="true" /> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/ic_tab_artists_white" /> 
</selector> 
this above xml is used to create tab widget. 

this is the output of my above code:- 

enter image description here

在我輸出的所有部件都顯示在屏幕中

,但我想在一次顯示4個選項卡,以及其他必須顯示滾動後。所以請建議我如何在我的java代碼中添加可滾動設施?

預先感謝您。

回答

5

請試試這個

HorizontalScrollView hr=new HorizontalScrollView(this); 
hr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

LinearLayout layout=new LinearLayout(this); 
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 

for(int i=0;i<100;i++){ 
    TextView txt=new TextView(this); 
    txt.setText("Text " + i); 
    layout.addView(txt); 
} 
hr.addView(layout); 

mainLayout.addView(hr); 

在上面的代碼中我添加了文本視圖。但您可以添加任何視圖而不是文本視圖。

1

在這裏你去:

ScrollableTabHost.java

ScrollableTabHost旨在表現得像TabHost,但有一個附加的滾動視圖,以適應更多的項目。

0

我遇到了這個鏈接,可能對你http://code.google.com/p/mobyfactory-uiwidgets-android/

編輯

有用,這是該項目Source

的來源,這是你如何使用它Add Tab

+0

謝謝先生提供給我的鏈接,但在該頁面中我找不到水平滾動視圖的概念,這是我的主要動機。請提供一些我可以在運行時進行水平滾動視圖的內容。 –