Android:如何制作水平滚动视图?

时间:2011-09-19 06:29:27

标签: android

我正在使用android,我想在运行时制作水平滚动视图,意味着我想通过java代码制作这个水平视图。

这是我的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代码中添加可滚动功能?

提前谢谢。

3 个答案:

答案 0 :(得分: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 :(得分:1)

你走了:

ScrollableTabHost.java

  

ScrollableTabHost的行为与TabHost类似,但附加滚动视图以适应更多项目。

答案 2 :(得分:0)

我遇到了这个可能对您有用的链接http://code.google.com/p/mobyfactory-uiwidgets-android/

修改

这是项目Source

的来源

这就是你如何使用它Add Tab