Note that there are some explanatory texts on larger screens.

plurals
  1. POMy button doesn't shows up with its background image's size When i using WRAP_CONTENT
    text
    copied!<p>Here is my <code>Button</code></p> <pre><code>convertView = mInflater.inflate(R.layout.guide_view_image_item, null); viewHolder = new ViewHolder(); viewHolder.myButton = (Button) convertView.findViewById(R.id.btn_goto_loginview); </code></pre> <p>Here is <code>guide_view_image_item.xml</code> with <code>btn_goto_loginview</code> inside</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" &gt; &lt;ImageView android:id="@+id/imgView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_delete" &gt; &lt;/ImageView&gt; &lt;Button android:id="@+id/btn_goto_loginview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/skep_button_in_guide_page" android:layout_alignParentBottom="true" android:layout_alignParentRight="true"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>skep_button_in_guide_page is a 182*182 PNG(32-bit color)6.10Kb Image</p> <p>my program works fine, but the myButton just shows up too big!(my ScreenWidth is 480 and this button's width coverd half of my ScreenWidth)</p> <p>Then i use </p> <pre><code>RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams(); </code></pre> <p>and i find that both <code>myButtonLayout.width</code> &amp; <code>myButtonLayout.height</code> equals to -2, I think maybe it means WRAP_CONTENT. Then i use </p> <pre><code>BitmapFactory.Options buttonOptions = new BitmapFactory.Options(); imageOptions.inJustDecodeBounds = true; BitmapFactory.decodeResource(EngageApp.getIns().getResources(), R.drawable.skep_button_normal, buttonOptions); </code></pre> <p>Then i find that <code>buttonOptions.outWidth</code> &amp; <code>buttonOptions.outHeight</code> both equals to 273 so i think it's the actual(real) width which is shown on the screen. It's strange, where does 273 comes from? it should be 182..</p> <p>Finally, I have to use this:</p> <pre><code>RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams(); myButtonLayout.width = 182; myButtonLayout.height = 182; viewHolder.myButton.setLayoutParams(myButtonLayout); </code></pre> <p>enforce set the width&amp;heigth to 182</p> <p>Then, Everything is ok, myButton got smaller which is my exact purpose. But I dont think It's the right way to solve this problem.</p> <p>Anyone can help me?? Thanks in advance.</p> <hr> <p>Maybe my description is confusing, Let me make it more clear.</p> <p>First, My Image is 182 width and i use WRAP_CONTENT, why my button doesn't shows up with 182 width on a 480 width screen directly. </p> <p>Second, why i got 273 when i using 'decodeResource'? 273 is not the width of the image(182 is,i don't even know where this number comes from), but it is the width that the button is actually showing. Help me explain this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload