0

在这里,我在 xml 中创建了 gridview gridView1 并在其中显示静态数据。

Q.1 是否可以在运行时生成 gridview 并将其添加到我的 Android 应用程序中,可能是 scrollviwe 或线性布局?

Q.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);

谢谢

4

1 回答 1

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>

您想FrameLayout用 myfragment.xml 替换上述布局(例如):

<?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课程......您可以在谷歌上搜索如何使用片段

但实际上你必须创建一个GridViewFragment扩展的类(例如)Fragment

于 2012-03-28T00:28:07.097 回答