2016-01-09 32 views
0

我有一個句子,用戶必須將他的數據放入句子(一個單詞)。如何將EditText放入TextView中

我試圖用LinearLayout和TextView(句子的開頭),EditText的ems = 5(這裏用戶應該把他的數據),然後TextView(句尾)(見下面的代碼)。但是在這種認識中,當我們使用長句並將它的一部分移動到下一行時,它看起來並不合適。

在模擬器,它看起來像:

|Blablabla ________ blablabla|  
|     blabla. | 

它把句子的末尾第二TextView的下方。但我需要的是第二線從新行的開頭開始,如:

|Blablabla ________ blablabla|  
|blabla.      | 

我試圖用從SVN-中心的解決方案:flow-text但得到的那種結果: enter image description here

我怎麼解決這個問題?感謝您收看本文!

XML:

<LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="4dp" 
     android:layout_marginTop="4dp"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingTop="15dp" 
      android:paddingRight="5dp" 
      android:textSize="20sp" 
      android:text="@string/firstPart_1" 
      android:id="@+id/textView1"/> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/name" 
      android:textSize="20sp" 
      android:inputType="textCapSentences" 
      android:paddingTop="2dp" 
      android:paddingBottom="6dp" 
      android:ems="7"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingTop="15dp" 
      android:paddingRight="5dp" 
      android:textSize="20sp" 
      android:text="@string/firstPart_2" 
      android:id="@+id/textView2"/> 

    </LinearLayout> 

XML(流文本):

<LinearLayout 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:orientation="vertical" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity"> 
<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="0dip" 
    android:layout_weight="1" > 

    <uk.co.deanwild.flowtextview.FlowTextView 
     android:id="@+id/ftv" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingRight="5dp" 
      android:textSize="20sp" 
      android:text="блабла" 
      android:id="@+id/textView1"/> 

     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/name" 
      android:textSize="20sp" 
      android:inputType="textCapSentences" 
      android:ems="5"/> 
     </LinearLayout> 
    </uk.co.deanwild.flowtextview.FlowTextView> 
</ScrollView> 
+0

如果您強制它們爲單行,文本視圖將包裝或截斷長字符串....我看不到一種方法 –

+0

爲什麼您沒有完整的textview並單擊在textview上打開一個對話框或彈出來輸入文字? –

回答

2

我認爲你應該使用的線性佈局(水平)爲單行。

 <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="fill in the blanks:" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Your name is: "/> 
     <EditText 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ems="5"/> 
    </LinearLayout> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Hello Brother, how are you" /> 
</LinearLayout> 

它工作正常。 :)

+0

這次我已經提供了完整的代碼......按照您的要求進行工作。 –

+0

對不起,但它完全不起作用,我想要的。我需要將TextView與EditText放在一行,之後,如果TextView太長,將它移動到第二行的開頭。 – Nikolai

+0

您只需在水平線性佈局中放置該數量的單詞(我們僅將它用於具有編輯文本的單行),以便它不會跳到第二行。剩餘的單詞可以放在水平線性佈局之後。 –

0

由於您的文本將如圖所示格式豐富,因此可以考慮使用WebView

在這種情況下,您可以使用HTML排列元素,並使用CSS樣式使用豐富文本。

此外,您還可以使用爲Java端和JavaScript端之間的通信提供的WebView機制,以便從該文本框到Java邏輯之間建立橋樑。 (Example

相關問題