2012-03-26 15 views
0

這裏我已經在xml中創建了gridview gridView1並在其中顯示靜態數據。
如何在運行時使用java在android中生成和添加gridview?

Q.1是否有可能在運行時生成gridview並將其添加到我的類似應用程序,也許在scrollviwe或線性佈局中?

問題.2如何更改顯示在gridview中的數據的字體大小?

請幫

String[] mydata = new String[] { 
        "Name", "Phone", 
        "Mangesh", "63737377", 
        "Rajnish", "63737344", 
        "Disha", "63737399", 
        "Ashwin", "63737312"}; 

      gridView = (GridView) findViewById(R.id.gridView1); 

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , mydata); 

感謝

+0

你需要在Android中使用片段來做到這一點。你可以在我們的佈局中放置一個佔位符組件,並在創建一個活動時將它與一個片段交換。這應該做到這一點。 – Ali 2012-03-26 06:47:55

+0

該怎麼做? – user1283358 2012-03-27 12:09:00

回答

0

你會宣稱您的佈局mylayout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainBack" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/transparent" > 

    <FrameLayout 
     android:id="@+id/gridViewFrag" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

你想從上面的佈局myfragment.xml取代FrameLayout(例如):

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:columnWidth="90dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 

然後在你的代碼,在適當的地方調用一個方法來換出的FrameLayout,並與你的GridView更換(不知道你是否可以在其他地方做)

private void attachGridViewFragment(int headerFrag) { 
    FragmentManager fragMgr = getSupportFragmentManager(); 
    FragmentTransaction xact = fragMgr.beginTransaction(); 
    try { 
     if (findViewById(headerFrag) != null) { 
      Resources res = getResources(); 
      String title = res.getString(R.string.app_icon_name); 
      xact.replace(headerFrag, new GridViewFragment(title, true, true), HEADER_FRAGMENT_TAG).commitAllowingStateLoss(); 
     } 
    } catch (NotFoundException e) { 
     // TODO Auto-generated catch block 
     Log.e("MyActivityName", e.getMessage()); 
    } 
} 

當然,你應得寫GridViewFragment班......那麼你可以只在Google上how to use fragments

但有效地,你必須創建一個類GridViewFragment(例如),擴展Fragment

相關問題