我试图改变我的gridview在我的android应用程序的字体大小。
这是我的代码,使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>
程序目前崩溃时,我试图让它应用我的风格,但它的工作原理发现,当它使用标准的风格。所以谁能帮我修复这个样式,这样我就可以设置字体大小了由于
ArrayAdapter
应具有TextView
类型的资源id
删除父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>
将您的自定义布局替换为上面的,将解决您的问题。
您可以创建自己的自定义适配器,如下所示:
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;
}
<<p> GridItem布局/strong> <?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类