动态将Edittext添加到现有editText下的相对布局 [英] Dynamically add Edittext to a relative layout under an existing editText

查看:113
本文介绍了动态将Edittext添加到现有editText下的相对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将editText动态添加到相对布局.布局已包含editText.我需要在现有的editText下方添加新的editText.

I am trying to add an editText dynamically to a relative layout. the layout contains an editText already. I need to add the new editText below the existing one.

EditText designation1 = new EditText(context);
designation1.setHint("designation");
designation1.setSingleLine(true);

我的布局是

layoutExp = 
    (RelativeLayout) childView.findViewById(R.id.relative_layout_edit_exp);

而我现有的修改文字是

designation = (EditText)childView.findViewById(R.id.editTextDesignatn);

推荐答案

您需要使用RelativeLayout.LayoutParams并用addRule(RelativeLayout.BELOW, ...)指定相对位置(这是android:below XML属性的程序等效项):

You need to use RelativeLayout.LayoutParams and specifiy the relative position with the addRule(RelativeLayout.BELOW, ...) (this is the programmatic equivalent for android:below XML attribute):

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.editTextDesignatn);
layoutExp.addView(designation1, params);

这篇关于动态将Edittext添加到现有editText下的相对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆