2012-02-11 66 views
4

我想知道如何调整编辑文本的高度来调整字体大小。我的意思是editText总是一样高,不管我是否将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> 

,但我也想伸展,如果有超过10行。我该怎么做?

[编辑]:

好吧,我解决它以这样的方式

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

     }); 
+0

好吧,也许例子可以帮助HTTP://dl.dropbox .com/u/18780140/edittexts.png当然在任何地方都有layout_height =“wrap_content”。问题是为什么“10sp editText”与默认的高度相同 – 2012-02-11 10:28:25

回答

3

集编辑文本高度WRAP_CONTENT

android:layout_height="wrap_content" 
+0

我说的是当我使用wrap_content,所以它不是我的解决方案 – 2012-02-11 10:21:26

0

如果你想的EditText高度超过一行你可以把你的EditText:

android:layout_height =“fill_parent”并设置android:l ayout_weight =“1”以获得更多空间用于在活动中添加更多视图

+0

但我只想在字符填充单行时拉伸。默认情况下,我想有1行 – 2012-02-11 10:23:23

+0

看到这个链接它可以帮助你http://stackoverflow.com/questions/1361332/how-to-make-android-edittext-smaller-than-default-in-height – Weloo 2012-02-11 10:34:43

2

您正在为EditText的高度提供20个dp约束。 你想要TextView的调整高度此外,默认情况单行文本大小使用

android:layout_height="wrap_content" 

的方面是假的。这确保了多行的真实性。

+0

20dp was只是例子,看评论 – 2012-02-11 10:30:19

0

可以使用滚动视图,并在滚动型添加EDITTEXT,并使用此:

android:inputType="textImeMultiLine" 
1

入住这

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