2014-10-02 55 views
0

新手Android程序員在這裏。我正在製作一個帶有EditText的xml佈局,EditText的寬度應該更改爲適合文本內部 - 文本可以由用戶或編程方式更改。我發現當文本超過4個字符時,EditText停止展開並將多餘的字符保持在屏幕外。 這裏是我的代碼:Android wrap_content不換行

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.appname.MainActivity" > 

    <EditText 
    android:id="@+id/textview1" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/seekbar1" 
    android:layout_alignLeft="@+id/textview2" 
    android:ems="10" 
    android:imeActionLabel="Done" 
    android:imeOptions="actionDone" 
    android:inputType="phone" 
    android:text="@string/NaN" /> 

    //Other objects... 

</RelativeLayout> 

閱讀WRAP_CONTENT的文檔,我還以爲它會做什麼,我想要的。我錯了嗎?謝謝您的幫助!

+0

你已經寫了android:layout_height =「wrap_content」。據我所知,你想寬度可擴展,而不是高度... – fran 2014-10-02 22:24:13

+0

你不能使用android:singleLine =「true」到你的edittext? – 2014-10-03 05:08:45

回答

0

第一你有固定的EditText的寬度都

的android:layout_width = 「100dp」 這應改爲WRAP_CONTENT。

其次,即使你這樣做,它不會工作,因爲包裝內容不起作用。如果必須動態更改EditText的大小,則必須擴展TextWatcher類並覆蓋ontextchanged()或afterTextChanged()方法。在這種方法中,您將不得不根據輸入的字符動態分配寬度屬性。

+0

謝謝,不敢相信我錯過了... 如果我將初始文本設置爲正確長度的佔位符,wrap_content會執行此操作嗎?例如。如果我知道動態設置的文本長度爲6,並且使初始文本爲「NNNNNN」,它會適合嗎? – 2014-10-03 18:57:58

+0

你想動態改變它,所以我不認爲它會工作..試試吧,讓我知道。順便說一句,如果它幫助我標記我的答案..如果你願意,我會得到20分:) – 2014-10-04 03:55:17