2016-09-27 195 views
-2

如何在Android應用中實現滾動和滾動條? 我是新來的android開發建議除了android開發者網站以外的一些網站快速學習。Android滾動視圖

+1

問題要求我們推薦或找到一本書,工具,軟件庫,教程或其他非本地資源,因爲它們傾向於吸引自以爲是的答案和垃圾郵件,所以不適合堆棧溢出。相反,請描述問題以及到目前爲止解決問題所做的工作。 – Lexi

+0

在youtube上搜索。 –

回答

0

很簡單,打開你的Android Studio中,創建新項目(空白項目)。然後在res/layout/activity_main.xml替換此代碼:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

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

     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="test1" /> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="test2" /> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="test3" /> 
     <TextView android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="test4" /> 
     . 
     . 
     . 
     . 
     . 

     <!-- something you want to display --> 
    </LinearLayout> 
</ScrollView> 

對於學習Android的,我建議這個網站,它有很多的乾淨清澈的例子讓你嘗試(它的舊的,但對我來說是很好的初學者):

https://www.tutorialspoint.com/android/index.htm

又一次谷歌是你最好的朋友。

0

這是一個簡單的任務,您可以通過使用ScrollView來實現此目的。把Layout放在一邊ScrollView,你就可以走了。有一件事ScrollView只能包含一個ChildView。

實施例:

<ScrollView 
    android:width="match_parent" 
    android:height="match_parent" > 

    <LinearLayout 
     ....> 
     . 
     . 
     . 
    </LinearLayout> 

</ScrollView> 
2

這裏是一個垂直滾動視圖的示例:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/scrollView" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button"/> 

     ... 
    </LinearLayout> 
</ScrollView> 

和水平滾動視圖:

<HorizontalScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/horizontalScrollView" 
    android:layout_below="@+id/scrollView" 
    android:layout_alignParentStart="true"> 

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

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Button" 
      android:id="@+id/button2"/> 

     ... 
    </LinearLayout> 
</HorizontalScrollView>