2012-02-11 63 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>