Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Android - CellRenderer

$
0
0

Hello everyone!

Need to customize the rendering of CellRender on android, I am using the following classes:

public class MenuCell : ViewCell
{
  public string Text { 
    get { 
      return label.Text; 
    }
    set { 
      label.Text = value;
    } 
  }

  public string Icon {
    get;
    set;
  }

  Label label;
  Image imageIcon;


  public MenuPage Host { get; set; }

  public MenuCell ()
  {
    label = new Label {
      YAlign = TextAlignment.Center,
      Font = Font.SystemFontOfSize(16.0f, FontAttributes.Bold),
      TextColor = Color.White,
    };

    imageIcon = new Image ();

    if (null != Icon) {
      imageIcon.SetBinding (Image.SourceProperty, Icon);
    }

    var layout = new StackLayout {
      BackgroundColor = Color.Black,
      Padding = new Thickness(20, 0, 0, 0),
      Orientation = StackOrientation.Horizontal,
      HorizontalOptions = LayoutOptions.StartAndExpand,
      Children = {imageIcon, label}
    };

    View = layout;
  }

  protected override void OnTapped ()
  {
    base.OnTapped ();
    Host.Selected (label.Text);
  }
}

Showing how to customize the image and label, I'm trying this:

[assembly: ExportRenderer (typeof (MenuCell), typeof (CustomCell))]
namespace Teste.Android
{
  public class CustomCell : CellRenderer
  {
    protected override View GetCellCore (Cell item, View convertView, ViewGroup parent, Context context)
    {
      var cell = base.GetCellCore(item, convertView, parent, context);
      cell.SetPadding(20, 30, 0, 30);


      var label = (TextView)base.GetCellCore(item, convertView, parent, context);
      label.SetTextColor(Color.White.ToAndroid());
      label.Text = ((MenuCell)item).Text;
      label.TextSize = Font.SystemFontOfSize(NamedSize.Large).ToScaledPixel() * 1.25f;
      label.Gravity = (global::Android.Views.GravityFlags.CenterVertical);
      label.SetTextColor(Color.FromHex("000000").ToAndroid());

      return cell;
    }

  }
}

Look at the attached image (Cell)

IOS worked perfect, but in android, I have difficulties, could someone help me?

Thank you!

Look at the attached image (Cell - iOS)


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>