2017-01-23 202 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 –