[英]Different size columns in a gridview xamarin C#
如何调整从列表中填充的 gridview 的列的大小。
下面是我的代码:
List<string> comanda = new List<string>();
GridView gv;
var gridview = FindViewById<GridView>(Resource.Id.gridView1);
//first row
comanda.Add("teeest");
comanda.Add("1");
comanda.Add("222");
comanda.Add("1");
comanda.Add("1");
//second row
comanda.Add("teeeeeeeeeest");
comanda.Add("1");
comanda.Add("222");
comanda.Add("1");
comanda.Add("1");
adapter2 = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSelectableListItem, comanda);
gridview.Adapter = adapter2;
像这样的axml:
<GridView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="382.5dp"
android:id="@+id/gridView1"
android:numColumns="5"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
/>
查看结果,以及我对这张图片的期望。 https://i.imgur.com/zB5hBA3.jpg
谢谢!
因为您将 GridView 的Adapter设置为系统定义的默认样式。
所以你需要一个自定义适配器。
创建一个名为 TextAdapter 的新 class ,它是BaseAdapter的子类:
public class TextAdapter : BaseAdapter
{
Context context;
List<string> Sources;
public TextAdapter(Context c , List<string> s)
{
context = c;
Sources = s;
}
public override int Count
{
get { return Sources.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return null;
}
public override long GetItemId(int position)
{
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public override View GetView(int position, View convertView, ViewGroup parent)
{
TextView textView;
if (convertView == null)
{
textView =new TextView(context);
textView.SetLines(1);
}
else
{
textView = (TextView)convertView;
}
textView.SetText(Sources[position],null);
return textView;
}
}
并在Activity中设置Adapter
gridview.Adapter = new TextAdapter(this,comanda);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.