调整EditText的字体大小

9

我想知道如何根据字体大小调整编辑文本的高度。我的意思是,无论我将字体大小更改为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) {         
            }

        });

好的,也许示例可以帮助解释 http://dl.dropbox.com/u/18780140/edittexts.png 当然,在每个地方都有layout_height="wrap_content"。问题是为什么“10sp editText”的高度与默认高度相同。 - Jakub Dyszkiewicz
7个回答

6

请检查这个

<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>

3

设置编辑文本的高度为wrap_content

android:layout_height="wrap_content"

我所描述的是当我使用wrap_content时,这对我来说不是解决方案。 - Jakub Dyszkiewicz

2
你正在为EditText的高度提供20dp的限制。 如果你希望textView根据文本大小调整高度,请使用。
android:layout_height="wrap_content"

默认情况下,单行为false。这确保了多行为true。

20dp只是一个例子,请看注释。 - Jakub Dyszkiewicz

0

你可以使用ScrollView,在其中添加你的EditText,并使用以下代码:

android:inputType="textImeMultiLine"

0

你可以添加 @style 页面名称

<style name="value_big" parent="@android:style/TextAppearance.Medium">
  <item name="android:textSize">35sp</item>
  <item name="android:textColor">@color/white</item>
  <item name="android:textStyle">bold</item>
  <item name="android:typeface">sans</item>
  <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
</style>

在你的编辑框中调用它

                  <EditText
                    android:id="@+id/msgtext"
                    android:layout_width="fill_parent"
                    style="@style/value_big"
                    android:layout_alignParentLeft="true"
                    android:layout_marginLeft="10dip"
                    android:layout_marginTop="10dip"
                    android:layout_marginRight="10dip"
                    android:inputType="textMultiLine"
                    android:layout_height="150dip"
                    android:gravity="top"
                    android:padding="5dip"> 
                  </EditText>

0

如果您想要让EditText的高度超过一行,您可以将以下代码放入您的EditText中:

android:layout_height="fill_parent" 并设置 android:layout_weight="1" 以获得更多的空间来添加在活动中的其他视图


但是我只想在字符填满单行时进行拉伸。默认情况下,我希望只有1行。 - Jakub Dyszkiewicz
请查看此链接,它可以帮助您:https://dev59.com/cXM_5IYBdhLWcg3wfTE0 - Weloo

-1
在editText中设置android:focusable="false"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接