Jak*_*icz 5 xml android android-edittext
我想知道如何将编辑文本的高度调整为te字体大小。我的意思是无论我将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>
Run Code Online (Sandbox Code Playgroud)
但如果有多于一条线,我也想拉伸。我该怎么做?
[编辑]:
好的,我是这样解决的:
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) {
}
});
Run Code Online (Sandbox Code Playgroud)
检查这个
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/editText1"
android:textSize="5sp"
android:inputType="textMultiLine"
android:text="hello h r u hello h r u hello h r u hello h r u hello h r u "
></EditText>
Run Code Online (Sandbox Code Playgroud)
将编辑文本高度设置为wrap_content
android:layout_height="wrap_content"
Run Code Online (Sandbox Code Playgroud)