Just like when programming Java for Android, LayoutParams does not
have a way to set dp or sp, the only thing it knows is px, so you have
to calculate it yourself. However this is quite simple.

In an activity:

    var scale = Resources.DisplayMetrics.Density;
    var pixels = (int) (dps * scale + 0.5f);

Where dps is the dp value you want.

In a View you can get the scale from the Context like:

    var scale = Context.Resources.DisplayMetrics.Density;



On Wed, Jun 27, 2012 at 9:18 PM, Sellec <echelo...@gmail.com> wrote:
> Sorry about my question, but i dont see any information in google.
> How can i set width like "30dp" for dynamically created label in my custom
> view?
> For example:
>  var label1 = new TextView(this.Context);
>  label1.LayoutParameters = new
> ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, 30);
>  label1.Gravity = GravityFlags.CenterVertical;
>  label1.SetText("test", TextView.BufferType.Normal);
>  this.AddView(label1);
> But this height of 30 is set with normal pixels and looks different on
> emulator and real device (screen dimensions is different). And i dont see
> any way to set a type of pixels for layoutparams. How can i do this?
>
>
> --
> View this message in context: 
> http://mono-for-android.1047100.n5.nabble.com/How-to-use-dip-or-dp-layout-dimension-for-LayoutParams-width-and-height-tp5710626.html
> Sent from the Mono for Android mailing list archive at Nabble.com.
> _______________________________________________
> Monodroid mailing list
> Monodroid@lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid



-- 
Med Venlig Hilsen / With Best Regards
Tomasz Cielecki
http://ostebaronen.dk
_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to