我正在尝试以编程方式设置自定义 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);
}
}
我看过一个类似的帖子,使用相同,但我无法使其工作。
更新:
鉴于快速响应,请在下面找到对场景的说明:
- 我有很多 EditText,它们是在活动布局中动态添加的。我需要从自定义控件而不是活动中执行此操作。我正在 EditText 中寻找正确的事件(如 onDraw、onPredraw 等),以便读取和设置布局参数。如果您找到要覆盖的适当事件,请告诉我。再次感谢!
- getLayoutParams 在视图 (EditText) 的构造函数中返回 null。所以,我认为当布局被实例化时,解决方案可能会解决正确的事件。
- 到目前为止,我已经尝试并阅读了很多事件,例如 onDraw、onLayout、onFinishInflate 等。但是参数被忽略或引发异常。
Tks 获取当前答案,TIA 获取任何进一步的答案!
弥尔顿。