嗨我正在設置一個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"
這樣做的工作包裝的內容。謝謝!但是,對於所有選項卡還有其他元素不通用。像我會設置默認的圖像大小,列寬等,而不是在java中顯式? – Lloyd 2010-12-02 16:02:01