我试图以编程方式设置自定义EditText组件的高度,但没有成功。我有一个从EditText继承的自定义类来设置自定义字体。 我想更新EditText的高度和填充,但我所有的意图都不会更新它。只有当我在XML文件中设置高度时,高度才会更新。如何以编程方式设置编辑文本的布局高度
这里的XML:
<example.ui.customcontrols.CustomEditText
android:id="@+id/txtUsername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:layout_marginTop="2dip"
android:inputType="text"
android:singleLine="true" />
这里定制的EditText的代码:
public class CustomEditText extends EditText {
public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.init();
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.init();
}
public CustomEditText(Context context) {
super(context);
this.init();
}
public void init() {
UIHelper.setTypeface(this);
this.setTextSize(17);
// This is not working.
this.setLayoutParams(newLayoutParams(LayoutParams.FILL_PARENT, 10));
this.setPadding(this.getPaddingLeft(), 0, this.getPaddingRight(), 0);
}
}
我见过similar post,使用相同的,但我不能”让它工作。
UPDATE:
鉴于快速反应,找到娄澄清的情景:
- 我有很多的EditText的,那是在活动布局动态添加。我需要从自定义控件中完成,而不是从活动中完成。我正在寻找EditText中的正确事件(如onDraw,onPredraw等),以便读取和设置布局窗体。如果您发现适当的事件被覆盖,请告诉我。再次!
- getLayoutParams在视图(EditText)的构造函数中返回null。所以,我认为当布局被实例化时,解决方案可能会解决正确的事件。
- 到目前为止,我已经尝试了解了很多事件,比如onDraw,onLayout,onFinishInflate等等,但是这个参数被忽略,或者导致异常。
Tks为当前答案,TIA为任何进一步的答案!
Milton。
嗨Shiv,谢谢你的回复。我忘了提及,但getLayoutParams方法返回null。我也试过了。 – Milton 2013-04-05 14:07:14