如何在选项卡式布局中显示Android网格视图

时间:2010-12-02 15:36:52

标签: android android-layout tabbed-view

嗨,我正在设置一个Android应用程序,我正在使用选项卡式界面。我想要三个标签,一个基于文本的标签(关于),一个webkit标签(商店)和一个图像的网格视图标签(图库),如Gridview tutorial

我的文本和webkit选项卡工作正常,但我无法找出在选项卡中格式化gridview的最佳方法。以tabbed interface tutrial为例,我在主类的onCreate()事件中声明了tab的内容。

public class Woodroid extends TabActivity  {
    /** Called when the activity is first created. */
    @Override
    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.setCurrentTab(0);

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, AboutActivity.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("about").setIndicator("",
                          res.getDrawable(R.drawable.ic_tab_about))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, StoreActivity.class);
        spec = tabHost.newTabSpec("store").setIndicator("Store",
                res.getDrawable(R.drawable.ic_tab_store))
            .setContent(intent);
        tabHost.addTab(spec);


        intent = new Intent().setClass(this, GalleryActivity.class);
        spec = tabHost.newTabSpec("gallery").setIndicator("Gallery",
                          res.getDrawable(R.drawable.ic_tab_gallery))
                      .setContent(intent);
        tabHost.addTab(spec);


        tabHost.setCurrentTab(0);
    }
}

那么在GalleryActivity.java中我有以下

public class GalleryActivity extends Activity {
  public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

            GridView gridview = new GridView(this);
            gridview.setAdapter(new ImageAdapter(this));

            setContentView(gridview);
     }
}

这是gridview教程的解释。那时缺少的是gridview布局定义。似乎我可以强制一些属性,如 gridview.setNumColumns(3); 但这不允许从gridview版本的/layout/main.xml中获得更灵活的equivelant

android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"

1 个答案:

答案 0 :(得分:0)

确保使用FrameLayout,cf tabbed example,设置android:layout_width =“WRAP_CONTENT”而不是fill_parent,然后在每个对象元素上使用重力和重量,直到它看起来如你所愿。