0
  1. This is the code.

    RelativeLayout layout = new RelativeLayout(this);
    TextView title = new TextView(this);
    title.setGravity(Gravity.CENTER);
    title.setWidth(320);
    title.setHeight(160)
    title.setText("hello");
    layout.addView(title);
    layout.setOnTouchListener(this);
    
  2. On the Screen, when I move the view location from (x:0, y:0) -> to (x: 200, y:300)

  3. And then set the TextView's value

    title.setText("aaaaabbbbbbccccccdddddddeeeeeeeddddd");
    

    the string length > textview's width, so multi-line display

  4. Then occurred error is "the TextView's location is back to (x:0, y:0)"

    if the TextView is display SingleLine, eg: title.setText("aaa"); the TextView's location is (x:200, y:300)

Is the android textView display multi-line strings, the layout is Redrawn? anyone can help me? I've been worried all night!!!

ps: the same error is also happened when call the TextView.bringToFront();

// In Android,if you changed the view location,must be setting layoutparams;otherwise,when the view reDraw,the view will back to location(0,0)。

// the solution is like this:

                RelativeLayout.LayoutParams lpFeedback = new RelativeLayout.LayoutParams(  
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
                lpFeedback.leftMargin = v.getLeft();  
                lpFeedback.topMargin = v.getTop();  
                lpFeedback.setMargins(v.getLeft(), v.getTop(), 0, 0);  
                v.setLayoutParams(lpFeedback);
Alex
  • 19
  • 2
  • 8
  • // when the view location changed,must be also change layoutparams RelativeLayout.LayoutParams lpFeedback = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); lpFeedback.leftMargin = v.getLeft(); lpFeedback.topMargin = v.getTop(); lpFeedback.setMargins(v.getLeft(), v.getTop(), 0, 0); v.setLayoutParams(lpFeedback); – Alex Aug 28 '17 at 09:31

1 Answers1

0

No it doesn't happen like that here is a sample code try this:

RelativeLayout layout = new RelativeLayout(this);
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        );
        TextView title = new TextView(this);
        RelativeLayout.LayoutParams tvlp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
        );
        tvlp.setMargins(200,300,0,0);
        title.setLayoutParams(tvlp);

        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setBackgroundColor(Color.GREEN);
        title.setWidth(320);
        title.setHeight(160);
        title.setText("helloaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        title.setTextSize(22);
        layout.addView(title);
        setContentView(layout, rlp);

Hope it helps!!!

sumit
  • 1,047
  • 1
  • 10
  • 15