Android的:如何取决于其他的编程设置TextViews位置? [英] Android : How to set TextViews positions depending on other ones programatically?
本文介绍了Android的:如何取决于其他的编程设置TextViews位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望把元素在我看来,这取决于其他的位置。让我来解释:首先,我有两个textviews,我已经成功地把第二个(villeOffre)之一的第一个(titreOffre):
titreOffre =(的TextView)findViewById(R.id.offreTitre);
titreOffre.setText(capitalizedString(offre.getTitle()));
villeOffre =(的TextView)findViewById(R.id.offreVille);
villeOffre.setText(offre.getLocality());
相关信息=(的TextView)findViewById(R.id.infos);ViewTreeObserver VTO = titreOffre.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(
{
@覆盖
公共无效onGlobalLayout()
{
FrameLayout.LayoutParams PARAMS =(FrameLayout.LayoutParams)villeOffre.getLayoutParams();
params.setMargins(15,titreOffre.getBottom()+ 12,15,0);
villeOffre.setLayoutParams(PARAMS);
。titreOffre.getViewTreeObserver()removeOnGlobalLayoutListener(本);
}
});
这完美的作品,但现在我希望把它取决于villeOffre位置的第三的TextView(相关信息)。那怎么办?
非常感谢。
解决方案
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
RR =新的RelativeLayout(本); B1 =新按钮(本);
b1.setId(R.id.Button01);
最近= B1;
b1.setText(点击我);
rr.addView(B1);
的setContentView(RR); b1.setOnClickListener(新View.OnClickListener(){ @覆盖
公共无效的onClick(视图v){
TV1 =新的TextView(can.this);
tv1.setId((int)的System.currentTimeMillis的());
LP =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW,recent.getId());
tv1.setText(时间:+ System.currentTimeMillis的());
rr.addView(TV1,LP);
最近= TV1;
}
});
}
I want to put elements in my view depending on the position of the other ones. Let me explain : First, I have two textviews and I've put successfully the second (villeOffre) one under the first one (titreOffre) :
titreOffre = (TextView) findViewById(R.id.offreTitre);
titreOffre.setText(capitalizedString(offre.getTitle()));
villeOffre = (TextView) findViewById(R.id.offreVille);
villeOffre.setText(offre.getLocality());
infos = (TextView) findViewById(R.id.infos);
ViewTreeObserver vto = titreOffre.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(
{
@Override
public void onGlobalLayout()
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) villeOffre.getLayoutParams();
params.setMargins(15,titreOffre.getBottom()+12,15,0);
villeOffre.setLayoutParams(params);
titreOffre.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
This works perfectly, but now I want to put a third textview (infos) which is depending on villeOffre position. How to do that ?
Thanks a lot.
解决方案
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rr = new RelativeLayout(this);
b1 = new Button(this);
b1.setId(R.id.Button01);
recent = b1;
b1.setText("Click me");
rr.addView(b1);
setContentView(rr);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tv1 = new TextView(can.this);
tv1.setId((int)System.currentTimeMillis());
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
recent = tv1;
}
});
}
这篇关于Android的:如何取决于其他的编程设置TextViews位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文