如何将 TextView 移动到relativeLayout中的x和y

发布于 2024-11-19 15:03:44 字数 3732 浏览 5 评论 0原文

我有一个相对布局,其中包含一些个人 TextView。我需要用 x 和 ay 放置这些文本视图,所以我这样做:

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

问题是我给出的 x 和 y 将是 textView 的左上角,而它应该是中心。

为了使其成为中心,我重写了 TextViewAnnotation 中的 onSizeChanged,并将 height/2 和 width/2 减去当前 y 填充和 x 填充:


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

但它什么也没做...我认为这是因为 setlayoutParams 采用 ViewGroup.LayoutParams 和不是RelativeLayout.LayoutParams。

我该如何修复它?


好的,我有一些消息。

我尝试使用RelativeLayout的addView(view,params)方法更改LayoutParams。 为此,我在 TextViewAnnotation 中创建了一个“控制器”,以便在触发 onSizeChanged 方法时调用此对象的方法:


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

如您所见,我尝试了此函数的 2 个版本:一个使用带参数的 addView,另一个不使用:一切都没有改变。

这次:

  • 我有2个textViewAnnotation,每个都调用函数“onSizeChanged”,但只有一个调用“AjusterAnnotation”函数,为什么?
07-13 18:05:18.086: 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
  • 唯一调用“AjusterAnnotation”的 textView 未绘制,即使 x、y、w 和 h 都可以......

请帮助 u_u

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.086: 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2024-11-26 15:03:44

嗯,这似乎是一种非常复杂的布局处理方式。

为什么使用 x 和 y 坐标而不是仅将重力设置为中心?

AbsoluteLayout 被弃用是有原因的 - 但您可能会阅读相关内容。但首先,您是否阅读过或任何其他布局教程?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文