[英]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.