How can I resize the columns of a gridview popualted from a list.
Below is my code:
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 like that:
<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"
/>
See the result, and what I expect in this image. https://i.imgur.com/zB5hBA3.jpg
Thanks!
Because you set the Adapter of GridView as the default style which define by system.
So you need a custom Adapter.
Create a new class called TextAdapter that subclasses 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;
}
}
And set the Adapter in Activity
gridview.Adapter = new TextAdapter(this,comanda);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.