问题描述
我有一个包含一些的personnal TextViews一个RelativeLayout的。我需要把带有X和Y的textviews,所以我做的:
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);
的问题是,我请x和y将TextView的的左上角,而应该是中心
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.
要使它的中心,我overrided的onSizeChanged在我TextViewAnnotation和高度。减去/ 2和宽度/ 2到当前y填充和X填充:
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);
但它确实没有什么......我想这是因为setlayoutParams采取ViewGroup.LayoutParams而不是RelativeLayout.LayoutParams。
But it does nothing... I think it's because setlayoutParams take a ViewGroup.LayoutParams and not a RelativeLayout.LayoutParams.
我怎样才能解决这个问题?
How can I fix it?
好吧,我有一些消息。
我试图与addView的RelativeLayout的(查看,则params)方法来改变的LayoutParams。
要做到这一点,我在TextViewAnnotation创造了一个控制器来调用这个对象的方法时,onSizeChanged方法被触发:
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);
}
正如你所看到的,我试过2个版本这个功能:一是使用addView使用参数,其他没有:没有什么改变。
As you can see, I tried 2 versions of this function: one using addView with params, the other without : nothing changed.
这时间:
- 我有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不是drawed这使得调用的唯一的TextView,即使X,Y,W和H都OK ......
请帮忙u_u
推荐答案
嗯,这似乎是处理布局的一种非常令人费解的方式。
Hmm, this seems like a very convoluted way of handling layout.
为什么要使用x和y坐标,而不是仅仅设置重力中心?
Why are you using x and y coordinates instead of just setting gravity to center?
AbsoluteLayout是pcated的一个原因去$ P $ - 但你也许能在读了。但首先,你有没有阅读或其他任何布局教程?
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?
这篇关于如何移动在一个RelativeLayout的一个TextView到x和一个ÿ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!