3

I am trying to change the font size of my gridview in my android application.

this is my code for makeing the gridview:

GridView gridview = (GridView)findViewById(R.id.gridView);
        ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(
                this,
                R.layout.customstyle, 
                Rows );

        gridview.setNumColumns(14);
        gridview.setAdapter(arrayadapter);

and here is the code from my custom style .xml document

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >

    <TextView
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="8dip" >
    </TextView>
</LinearLayout>

The program currently crashes when i try and get it to apply my stlye but it workss find when it uses the standard styles. so can someone help me fix this style so i can set the font size Thanks

4

2 に答える 2

3

ArrayAdapterタイプのリソース ID を持つ必要がありTextViewます。

親を削除するLinearLayoutと、レイアウトは次のようになります

このように使う

customStyle.xml

<?xml version="1.0" encoding="utf-8"?>
  <TextView
         xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/grid_item_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="8sp" >
    </TextView>

カスタムレイアウトを上記のものに置き換えて、問題を解決してください。

于 2013-04-17T16:33:55.380 に答える
1

次のように独自のカスタム アダプタを作成できます。

public class MyAdapter extends BaseAdapter{

private Context mContext;
private String[] text = {};
private Integer[] mThumbIds = {};
public MyAdapter(Context c,String[] text)
{
    mContext = c;
    this.text = text;
}

public int getCount()
{
    return mThumbIds.length;
}

public Object getItem(int position)
{
    return position;
}

public long getItemId(int position)
{
    return position;
}

public View getView(int position, View convertView, ViewGroup parent)
{
    View myView = convertView;
    if (convertView == null)
    {
        LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myView =li.inflate(R.layout.grid_item, null);
        TextView tv = (TextView)myView.findViewById(R.id.grid_itemTxt);
        tv.setText(text[position]);
    }
    return myView;
}

GridItem レイアウト

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id = "@+id/gridItem"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation= "vertical"
    android:gravity="center_horizontal" >

    <TextView 
        android:id="@+id/grid_itemTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textColor="#6075B2">
    </TextView>
</LinearLayout>

次に、 onCreate() メソッドで:

private GridView grid;
private String text[] = {"hello","world","here","is","a","grid","view"};
grid = (GridView)findViewById(R.id.yourGridLayoutXml);
grid.setAdapter(new MyAdapter(this, text));

:MyAdapterは拡張する Java クラスです。BaseAdataper

于 2013-04-17T16:30:12.690 に答える