snipt

Ctrl+h for KB shortcuts

Text only

converter units to dp margin

1
2
3
4
5
6
7
final float scale = getContext().getResources().getDisplayMetrics().density;
                                int pixels = (int) (42 * scale + 0.5f);
                                
                                
 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(150, 150);
 layoutParams.leftMargin = pixels;
 iv_item_h.setLayoutParams(layoutParams);
https://snipt.net/embed/2195d6fe865e06f3baf4b6c55585997a/
https://snipt.net/raw/2195d6fe865e06f3baf4b6c55585997a/
2195d6fe865e06f3baf4b6c55585997a
text
Text only
7
2016-09-27T08:01:41
True
False
False
Apr 30, 2016 at 09:54 AM
/api/public/snipt/145924/
converter-units-to-dp-margin
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a>final float scale = getContext().getResources().getDisplayMetrics().density; </span><span id="L-2"><a name="L-2"></a> int pixels = (int) (42 * scale + 0.5f); </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a> LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(150, 150); </span><span id="L-6"><a name="L-6"></a> layoutParams.leftMargin = pixels; </span><span id="L-7"><a name="L-7"></a> iv_item_h.setLayoutParams(layoutParams); </span></pre></div> </td></tr></table>
android, dp, imageview, margin
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.