Search code examples
androidandroid-layoutandroid-relativelayoutandroid-custom-view

RelativeLayout addView LEFT_TO Center View,Then the Center View doesn`t still in the center,why?


as the title,picture as below

enter image description here

code:

textView = new TextView(mContext);
textView.setId(5005);
textView.setText(mShareText);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mShareTextSize);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.alignWithParent = true;
params.addRule(CENTER_IN_PARENT, TRUE);
addView(textView, params);

imageView = new ImageView(mContext);
initImageView();
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.addRule(LEFT_OF, 5005);
params2.addRule(CENTER_VERTICAL, TRUE);
params2.rightMargin = mDrawablePaddingDP;
addView(imageView, params2);

setControlAble();

This is Custom View which extends RelativeLayout . I want the M still in the center, the program left to the M,how should I do?


Solution

  • Create one more text view, give it an ID and set text as " "(space or blank). Now set it in mid. Now you can set your image and text view left to it.Best of luck.