2014-01-13 161 views
1

我爲我的測試應用程序,一個TextView,一個ImageView和一個TextView設計了一個簡單的佈局。但第二個TextView我希望它可以滾動(通過觸摸)。ScrollView在Android佈局中不起作用

我已經在網上搜索並嘗試了很多解決方案,但仍無法使其工作。

<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" 
    android:scrollbars="vertical" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="@string/title" 
     android:textSize="40sp" 
     android:textStyle="bold" /> 


     <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/title" 
     android:layout_centerHorizontal="true" 
     android:adjustViewBounds="true" 
     android:contentDescription="@string/ada_lovelace" 
     android:paddingTop="8dp" 
     android:src="@drawable/ada_lovelace" /> 

    <ScrollView 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:id="@+id/scroll1" 
     android:layout_below="@id/imageView1"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingTop="8dp" 
      android:text="@string/hello_world" 
      android:textSize="22sp" /> 


    </ScrollView> 


</RelativeLayout> 

請幫幫我。

+0

它的工作原理只有在TextView中有多個元素,滾動 –

+0

參考此鏈接,它解決了我的問題.... HTTP ://stackoverflow.com/questions/1748977/making-textview-scrollable-in-android .. – krishna

+0

使用ListView和簡單的適配器 – user1755546

回答

0

你不需要ScrollViewTextView,TextView滾動屬性。

嘗試設置它並查看它是否有效。

0

您可以使用this鏈接。而在XML使用滾動視圖的格式如下:

<linearlayout> 
     <scrollview> 
     <linear>or <relativelayout> 
     </linear layout> 
     </scrollview> 
    </linear layout>. 
0
  1. 設置了滾動的高度38dp。
  2. 將TextView的大小設置爲40sp。

然後你可以滾動TextView。

0

滾動運作,只有當其子的一部分是不是在屏幕上可見,由於大尺寸或大量

0

我有同樣的問題幾個星期前,這是我的解決方案:

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RelativeLayout 
     android:id="@+id/RelativeLayout2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/textview" /> 
    </RelativeLayout> 
</ScrollView> 

但是,如果TextView中沒有「足夠」的內容,則它不可滾動。只有當它超過你的屏幕卡恩「捕捉」。

+0

仍然不適合我。 hello_world字符串我改變了一些段落,並且它有足夠長的時間來滾動,但它不會移動:( – user3189534

0

重新導入我的項目後,一切工作正常。我不知道這是什麼錯誤,但也許是因爲日食錯誤。

0

而不是使用ScrollView爲您的TextView,您可以使用以下代碼使您TextView可滾動。

<TextView 
    android:id="@+id/txtView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:maxLines="5" 
    android:scrollbars="vertical" 
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

而且在你的Java文件使用此代碼

txtView.setMovementMethod(new ScrollingMovementMethod()); 

希望這會有所幫助。

0

滾動變成僅是滾動型

之間的部分所以,如果你想要滾動的一切(正如我理解你這樣做),你只需要改變的序列。 我也相信ScrollView需要在佈局和佈局,所以你需要兩個佈局:一個裏面和一個out.Though我不知道最後一句 - 我剛開始學東西

-1

這是您的解決方案實現此代碼並運行代碼textview是可滾動的。

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/title" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true" 
    android:contentDescription="@string/ada_lovelace" 
    android:paddingTop="8dp" 
    android:src="@drawable/ada_lovelace" /> 

<ScrollView 
    android:layout_height="fill_parent" 
    android:layout_width="wrap_content" 
    android:id="@+id/scroll1" 
    android:layout_below="@id/imageView1"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="8dp" 
     android:text="@string/hello_world" 
     android:textSize="22sp" /> 

    </RelativeLayout> 
</ScrollView> 

+0

請直接在您的答案中包含代碼。 –

0

它應該工作請嘗試

<ScrollView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding_bottom = "10dp"> 
<TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/textview" /> 
</ScrollView>