试图在android中改变gridview中的字体大小

时间:2013-04-17 16:27:55

标签: android gridview styles font-size

我想在我的Android应用程序中更改gridview的字体大小。

这是我制作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);

这是我的自定义样式.xml文档中的代码

<?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>

当我试图让它应用我的stlye时,程序当前崩溃了,但是当它使用标准样式时它会找到它。 所以有人可以帮我修复这种风格,所以我可以设置字体大小 感谢

2 个答案:

答案 0 :(得分: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>

使用上述方法替换您的自定义布局,以解决您的问题。

答案 1 :(得分: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是一个扩展BaseAdataper

的java类