2013-04-05 98 views
1

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

鑑於快速反應,找到婁澄清的情景:

  1. 我有很多的EditText的,那是在活動佈局動態添加。我需要從自定義控件中完成,而不是從活動中完成。我正在尋找EditText中的正確事件(如onDraw,onPredraw等),以便讀取和設置佈局窗體。如果您發現適當的事件被覆蓋,請告訴我。再次!
  2. getLayoutParams在視圖(EditText)的構造函數中返回null。所以,我認爲當佈局被實例化時,解決方案可能會解決正確的事件。
  3. 到目前爲止,我已經嘗試瞭解了很多事件,比如onDraw,onLayout,onFinishInflate等等,但是這個參數被忽略,或者導致異常。

Tks爲當前答案,TIA爲任何進一步的答案!

Milton。

回答

0
LayoutParams params = layout.getLayoutParams(); 
// Changes the height and width to the specified *pixels* 
params.height = 100; 
params.width = 100; 
+0

嗨Shiv,謝謝你的回覆。我忘了提及,但getLayoutParams方法返回null。我也試過了。 – Milton 2013-04-05 14:07:14

1

試着從包含CustomEditText的佈局中做到。例如:

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
    > 
<com.example.untitled1.CustomEditText 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello World, MyActivity" 
     android:id="@+id/et" 
     /> 

MyActivity.java

public class MyActivity extends Activity { 

private CustomEditText et; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    et= (CustomEditText) findViewById(R.id.et); 
    ViewGroup.LayoutParams lp = et.getLayoutParams(); 
    lp.width = 100; 
    lp.height = 100; 
    et.setLayoutParams(lp); 
} 

}

就像一個魅力的工作!

+1

嗨jimpanzer,謝謝你的回覆。正如我對Shiv所說的,我忘了提及,但getLayoutParams方法返回null。我也試過了。 – Milton 2013-04-05 14:07:54

+0

@Milton我更新了我的答案。覈實。 – jimpanzer 2013-04-05 14:31:26

+0

在我的情況下,我有很多EditText,它們是在活動佈局中動態添加的。我需要從自定義控件中完成,而不是從活動中完成。我正在尋找EditText中的正確事件(如onDraw,onPredraw等),以便讀取和設置佈局窗體。如果您發現適當的事件被覆蓋,請告訴我。再次! – Milton 2013-04-05 14:34:48

相關問題