【发布时间】:2012-02-11 10:01:48
【问题描述】:
我想知道如何将编辑文本的高度调整为字体大小。我的意思是无论我将 fontSize 更改为 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) {
}
});
【问题讨论】:
-
好的,也许示例会有所帮助dl.dropbox.com/u/18780140/edittexts.png 当然到处都有 layout_height="wrap_content" 。问题是为什么“10sp editText”与默认高度相同
标签: android xml android-edittext