Using setLeft / setRight Methods in New Text View

I am working with TextViews progammatically and I need to dynamically add new views and set their left / top position in the parent RelativeLayout. What I am doing looks something like this:

RelativeLayout global=(RelativeLayout)findViewById(R.id.global);
TextView view=(TextView)findViewById(R.id.root);
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView childView=new TextView(view.getContext());
lp.addRule(RelativeLayout.ABOVE, view.getId());
childView.setLayoutParams(lp);
childView.setId(childView.getId()+1);
childView.setText("TRY STRING");
childView.setTextSize(view.getTextSize());
childView.setWidth(view.getWidth());
childView.setLeft(view.getLeft()+interval);
global.addView(childView);

      

Basically, I have a text view at a specific position (which is called a view) and I am trying to create a new text above the existing one and at the same left position. Although the getLeft () method correctly returns the left view position, the child has a left margin set to zero and appears at the left end of the screen when I add it to the RelativeLayout. It looks like it completely ignores the setLeft method! Can anyone explain why? Thank you so much in advance!

+3


source to share


1 answer


public final void setLeft (int left)

Added in API Level 11 Sets the left position of this view relative to its parent. This method is intended to be called by the layout system and should not normally be called otherwise, as the property can be changed at any time across the layout.

Parameters left At the bottom of this view in pixels.


Most likely the left value is set by the layout every time it is drawn and overrides the value you set in your view.



animated layouts are very similar to XML and even when executed programmatically.

try using

lp.setMargins(int left, int top, int right, int bottom);

      

.. or some other method that will not be canceled when drawing the layout

+4


source







All Articles