2013-04-05 75 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

相关问题