我想知道如何根据字体大小调整编辑文本的高度。我的意思是,无论我将字体大小更改为5sp还是保留默认值,editText的高度始终相同。当然,我可以使用类似以下的内容:
<EditText
android:id="@+id/msgInput"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:inputType="textMultiLine"
android:textSize="14sp"
android:hint="@string/hintMsgInput">
</EditText>
但是如果有超过1行的文本,我也希望能够进行拉伸。我该怎么做呢?
[编辑]:
好的,我用以下方法解决了这个问题:
msgInput = (EditText) findViewById(R.id.msgInput);
msgInput.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s) {
if (msgInput.getLineCount() > 1)
{
msgInput.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
}
else
{
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
msgInput.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, height));
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});