Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically adding ABOVE to LayoutParams causes view height to be 0
    primarykey
    data
    text
    <p>I have a custom <code>View</code>. In the constructor for the view, I create and add two subviews. However, using <code>LayoutParams.addRule()</code> is causing some problems. Rules such as <code>CENTER_HORIZONTAL</code> work, but when I try to use <code>ABOVE</code>, the subview ends up with a height of 0.</p> <p>Here is the code in my constructor:</p> <pre><code> setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); mLayoutParams = new LayoutParams(width, height); mLayoutParams.leftMargin = left; mLayoutParams.topMargin = top; mImage = new ImageView(getContext()); mImage.setImageDrawable(getResources().getDrawable(R.drawable.my_image_drawable)); mImage.setScaleType(ScaleType.FIT_XY); mImage.setId(R.id.my_image_id); addView(mImage, mLayoutParams); mText = new TextView(getContext()); mText.setText(R.string.my_text); mText.setId(R.id.my_text_id); LayoutParams textParams = new LayoutParams(200, 40); // textParams.addRule(CENTER_HORIZONTAL, TRUE); //Works textParams.addRule(ABOVE, mImage.getId()); //Doesn't work addView(mText, textParams); </code></pre> <p>If I inspect the view on an emulator with the Device Monitor, I can see that the <code>layout_height</code> of the text is 40, but the top, bottom, and height parameters are all 0. <code>measuredHeight</code> appears to be the same as the screen width.</p> <ol> <li>Am I doing something wrong with adding the rules?</li> <li>Is there a better place to add the rules? Does something need to happen first?</li> <li>Any other alternatives? Making the <code>TextView</code> have a height of FILL_PARENT, gravity BOTTOM, and setting its bottom margin to the height of the image works, but I need to be able to add another view above the text, and this won't work for that.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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