简体   繁体   中英

How to set layout height of an edittext programmatically

I'm trying to set the height of a custom EditText component programmatically, without success. I have a custom class that inherit from EditText, to set a custom font. I want to update the height and padding of the EditText, but all my intents don't update it. Only when I set the height in the XML file, the height is updated.

Here the 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" />

Here the code of the custom 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);
    }
}

I've seen a similar post , that use the same, but I couldn't make it work.

UPDATE:

Given the quick responses, find bellow a clarification of the scenario:

  1. I've a lot of EditText, that are added dynamically in the activity layout. I need to do that from the custom control, rather from the activity. I'm looking for the right event in the EditText (like onDraw, onPredraw, etc), in order to read and set the layoutparams. Let me know if you find the proper event to be overrided. Tks again!
  2. getLayoutParams return null in the constructor of the view (EditText). So, i think that the solution may come addressing the right event, when the Layout be instantiated.
  3. So far I've tried and read about a lot of the events, like onDraw, onLayout, onFinishInflate, etc. But the param is ignored, or trows an exception.

Tks for the current answers, and TIA for any further answer!

Milton.

Try do it from layout, which contain CustomEditText. For example:

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);
}

}

Work like a charm!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM