Xamarin: Create a custom grid View

懵懂的女人 提交于 2020-01-14 00:33:54

问题


I need create a custom GridView in Android Application (Dev. on VS2012 with Xamarin plugin). I need on each cell a image and label text.

This example work perfectly, but has not a text label. http://developer.xamarin.com/guides/android/user_interface/grid_view/

I need anything how this: Custom Grid View {Image+Text}

but I only found Java Examples. Please Help me

PD: Sorry by Image, but I can't upload images (I need more reputation).


回答1:


Create a new layout that has the ImageView & TextView. Then, in the GetView method of the ImageAdapter class, inflate that layout and populate it, for example:

public override View GetView (int position, View convertView, ViewGroup parent)
{
    View view;

    if (convertView == null) 
    {  
        view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.my_grid_row, parent, false);
    }
    else
    {
        view = convertView;
    }


    var imageView = view.FindViewById<ImageView>(Resource.Id.my_image_view);
    imageView.SetImageResource (thumbIds[position]);

    var textView = view.FindViewById<TextView>(Resource.Id.my_text_view);
    textView.Text = "Text to Display";

    return view;
}

Edit: (Add to Code "(" and ";")

Update: Here is a sample layout file, my_grid_row.axml, that is inflated by the GetView method:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="centerCrop"
        android:id="@+id/my_image_vieww" />
    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/my_text_view" />
</LinearLayout>


来源:https://stackoverflow.com/questions/28464775/xamarin-create-a-custom-grid-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!