0

ここでは、xmlでgridview gridView1を作成し、静的データを表示しています。

Q.1 実行時にgridviewを生成して、scrollviweまたは線形レイアウトでandoidアプリに追加することは可能ですか?

Q.2グリッドビューに表示されるデータのフォントサイズを変更するにはどうすればよいですか?

助けてください

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クラスを作成する必要があります...フラグメントの使用方法については、 Googleだけでできます。

しかし、事実上、GridViewFragment(たとえば) を拡張するクラスを作成する必要がありますFragment

于 2012-03-28T00:28:07.097 に答える