开发者

How to move a TextView to an x and an y in a relativeLayout

开发者 https://www.devze.com 2023-03-19 05:21 出处:网络
I have a relativelayout which contains some personnal TextViews. I need to place those textviews with an x and a y, so I do that:

I have a relativelayout which contains some personnal TextViews. I need to place those textviews with an x and a y, so I do that:

RelativeLayout layoutAnnotations = (RelativeLayout)findViewById(R.id.affichageFiche_layoutAnnotations);

TextViewAnnotation tvAnno = new TextViewAnnotation(this,this,anno.getTexte(),anno.getFontSize());

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = x;
params.topMargin = y;
layoutAnnotations.addView(tvAnno, params);

The problem is that the x and y I give will be the top left corner of the textView, while it should be the center.

To make it the center, I overrided the onSizeChanged in my TextViewAnnotation, and substract height/2 and width/2 to the current y padding and x padding:


protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   Log.d("TextViewAnnotation", "onSizeChanged");
   super.onSizeChanged(w, h, oldw, oldh);
   if (w ==0 || h==0) return;

   RelativeLayout.LayoutParams old_params = (RelativeLayout.LayoutParams)getLayoutParams();
   int old_x = old_params.leftMargin;
   int old_y = old_params.topMargin;

   Log.d("AffFiche", "Ancien : x:"+old_x+" y:"+old_y+" w:"+w+" h:"+h);

   RelativeLayout.LayoutParams new_params = new RelativeLayout.LayoutParams(w, h);
   new_params.leftMargin = old_x - w/2;
   new_params.rightMargin = old_y - h/2;
   Log.d("AffFiche", "Nouveau : x:"+new_params.leftMargin+" y:"+new_params.rightMargin+" w:"+w+" h:"+h);

   setLayoutParams(new_params);

But it does nothing... I think it's because setlayoutParams take a ViewGroup.LayoutParams and not a RelativeLayout.LayoutParams.

How can I fix it?


ok, I have some news.

I tried to change the LayoutParams with the addView(view,params) method of the RelativeLayout. To do that, I created a "controller" in my TextViewAnnotation to call a method of this object when the onSizeChanged method is fired:


//in TextViewAnnotation class
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.d("TextViewAnnotation", "onSizeChanged");
        super.onSizeChanged(w, h, oldw, oldh);
        if (w ==0 || h==0) return;
        controller.ajusterAnnotation(this,w,h);
    }

//in my controller class
public void ajusterAnnotation(TextViewAnnotation annotation,int w,int h){
        Log.d("AffFiche", "AjusterAnnotation "+annotation);
        RelativeLayout layoutAnnotations = (RelativeLayout)findViewById(R.id.affichageFiche_layoutAnnotations);

        RelativeLayout.LayoutParams old_params = (RelativeLayout.LayoutParams)annotation.getLayoutParams();
        int old_x = old_params.leftMargin;
        int old_y = old_params.topMargin;
        Log.d("AffFiche", "Ancien : x:"+old_x+" y:"+old_y+" w:"+w+" h:"+h);

        RelativeLayout.LayoutParams new_params = new RelativeLayout.LayoutParams(w, h);
        new_params.leftMargin = old_x - w/2;
        new_params.rightMargin = old_y - h/2;
        Log.d("AffFiche", "Nouveau : x:"+new_params.leftMargin+" y:"+new_params.rightMargin+" w:"+w+" h:"+h);

        layoutAnnotations.removeView(annotation);
        //layoutAnnotations.addView(annotation, new_params);
        annotation.setLayoutParams(new_params);
        layoutAnnotations.addView(annotation);
    }

As you can see, I tried 2 versions of this function: one using addView with params, the other without : nothing changed.

This time:

  • I have 2 textViewAnnotation, each one call the function "onSizeChanged", but only one call the "AjusterAnnotation" function, why?
07-13 18:05:18.0开发者_开发百科86: DEBUG/TextViewAnnotation(980): onSizeChanged
07-13 18:05:18.086: DEBUG/AffFiche(980): AjusterAnnotation affichageFiche.TextViewAnnotation@407405a0
07-13 18:05:18.106: DEBUG/AffFiche(980): Ancien : x:117 y:236 w:87 h:10
07-13 18:05:18.106: DEBUG/AffFiche(980): Nouveau : x:74 y:231 w:87 h:10
07-13 18:05:18.115: DEBUG/TextViewAnnotation(980): onSizeChanged
  • the only textView which made call to "AjusterAnnotation" is not drawed, even if the x,y,w and h are ok...

Please help u_u


Hmm, this seems like a very convoluted way of handling layout.

Why are you using x and y coordinates instead of just setting gravity to center?

AbsoluteLayout was deprecated for a reason - but you might read up on that. But first, have you read this or any of the other Layout tutorials?

0

精彩评论

暂无评论...
验证码 换一张
取 消