2017-01-23 204 views
2

我在我的LinearLayout中設置了ScrollView。我想要一個TextView滾動。我試圖在xml中使用ScrollView。但是,它不工作。android textview scrollview在佈局滾動視圖

<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:padding="8dp" 
    tools:context=".SubpagesLayout.ReportResult"> 

    <!-- first scrollView --> 
    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <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:background="@drawable/edit_corner_blue" 
       android:gravity="center" 
       android:text="@string/personalInformation" 
       android:textColor="@android:color/white" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="8dp" 
       android:orientation="horizontal"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/birthDate" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text=" TestData" /> 

      </LinearLayout> 

      <!-- second scrollView --> 
      <ScrollView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 
        <TextView 
         android:id="@+id/textOphthalmoscopy" 
         android:layout_width="match_parent" 
         android:layout_height="80dp" 
         android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." /> 
      </ScrollView> 
     </LinearLayout> 
    </ScrollView>  
</LinearLayout> 

我想這樣的一種方式,但它仍然沒有工作過。

android:maxLines = "AN_INTEGER" 
android:scrollbars = "vertical" 

我用

myTextView.setMovementMethod(new ScrollingMovementMethod()); 

因此,如何讓TextView滾動?如果是第二個ScrollView

+1

你不能使用嵌套的滾動視圖,請嘗試使用由Android提供的NestedScrollView,但是避免嵌套滾動如果U可以 – Ak9637

+0

是的,這NestedScrollView!感謝您的幫助,但爲什麼我應該避免NestedScrollView? –

+0

我不知道這個技術的答案,但物理上它不建議在另一個滾動表面有一個滾動,因爲android是基於MATERIAL UI – Ak9637

回答

1

您可能會考慮使用NestedScrollView以及ListView來顯示可滾動的TextView

ListView將有一個固定的大小,以便它不會佔用內部項目的總高度。

所以佈局可能看起來像這樣。

<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:padding="8dp" 
    tools:context=".SubpagesLayout.ReportResult"> 

    <!-- first scrollView --> 
    <NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <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:background="@drawable/edit_corner_blue" 
       android:gravity="center" 
       android:text="@string/personalInformation" 
       android:textColor="@android:color/white" /> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="8dp" 
       android:orientation="horizontal"> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/birthDate" /> 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text=" TestData" /> 
      </LinearLayout> 

      <ListView 
       android:layout_width="match_parent" 
       <!--set a fixed height--> 
       android:layout_height="80dp" 
       .. Other attributes 
      </ListView> 
     </LinearLayout> 
    </NestedScrollView>  
</LinearLayout> 

而且ListView的列表項可能包含TextView要滾動。將TextView的高度設置爲wrap_content

<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:padding="8dp" 
    tools:context=".SubpagesLayout.ReportResult"> 

     <TextView 
      android:id="@+id/textOphthalmoscopy" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." /> 
</LinearLayout> 

現在你ListView將有一個單一的項目,這將與列表的默認行爲滾動。

整個佈局將滾動通過NestedScrollView

希望有幫助!

+0

哈,它又是你!非常感謝您的幫助。這種方式添加一個新的佈局,它更復雜,我反正,謝謝@Reaz Murshed –

+0

很高興再次在這裏找到你。請注意,如果有幫助。 :) –

1

在另一個scrollView裏面有一個scrollView並不是一個好的規則,但是你可以在下面檢查鏈接它可以解決你的問題。

Solution 1

Solution 2

+0

我已經創建瞭解決方案,謝謝@Anti DS –