androidtextviewdrawmultilinebringtofront

When Android Textview Draw Multi-Line strings, The Layout Redrawn?


  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);

Solution

  • 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!!!