如何在选项卡式布局中显示Android Gridview
问题描述:
嗨我正在设置一个Android应用程序,并使用了选项卡式界面。我想要三个标签,一个基于文本的标签(About),一个webkit标签(Store)以及一个图像(Gallery)的GridView标签(如Gridview tutorial)。如何在选项卡式布局中显示Android Gridview
我有文本和webkit选项卡工作正常,但我无法弄清楚在选项卡内格式化gridview的最佳方式。以tabbed interface tutrial为例,我在main类的onCreate()事件中声明了标签的内容。
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);
的属性,但不允许用于更灵活的寻找equivelant从GridView的版本/layout/main.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
答
确保使用的FrameLayout,比照 tabbed example,设置android:layout_width =“WRAP_CONTENT”而不是fill_parent,然后在每个对象元素上使用重力和重量进行播放,直到它看起来如何。
这样做的工作包装的内容。谢谢!但是,对于所有选项卡还有其他元素不通用。像我会设置默认的图像大小,列宽等,而不是在java中显式? – Lloyd 2010-12-02 16:02:01