我試圖以編程方式設置自定義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