How to display an Android gridview within a tabbed layout
Hi I'm setting up an android app and I'm using a tabbed interface. I'd like three tabs, a text based tab (About), a webkit tab (Store) and a gridview tab of images (Gallery) like in the Gridview tutorial.
I have the text and webkit tab working fine, but I cannot figure out the best way to format the gridview within the tab. Using the tabbed interface tutrial as an example, I am declaring the tab's content within the onCreate() event of the main class.
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);
开发者_Python百科 tabHost.setCurrentTab(0);
}
}
So then in the GalleryActivity.java I have the following
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);
}
}
Which is an interpretation from the gridview tutorial. The thing that is missing then is the gridview layout definition. It seems I can force some of the attributes like
gridview.setNumColumns(3);
but that does not allow for the more flexible looking equivelant from the /layout/main.xml of the gridview version
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
Make sure to use a FrameLayout, c.f. tabbed example, set android:layout_width="WRAP_CONTENT" instead of fill_parent and then play with gravity and weight on each object element until it looks how you want it to.
精彩评论