2016-09-27 293 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>