📜  Android中的ScrollView

📅  最后修改于: 2021-05-10 14:47:14             🧑  作者: Mango

在Android中,ScrollView是一个视图组,用于创建垂直可滚动视图。滚动视图仅包含一个直接子级。为了在滚动视图中放置多个视图,需要将一个视图组(如LinearLayout)作为直接子级,然后我们可以在其中定义许多视图。 ScrollView仅支持垂直滚动,因此为了创建水平可滚动视图,使用HorizontalScrollView。

ScrollView的XML属性

Attribute

Description

android:fillViewport Defines whether the scrollview should stretch its content to fill the viewport.

继承的属性

从FrameLayout

Attributes

Description

android:measureAllChildren Determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false.

从视图

Attributes

Description

android:alpha alpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque).
android:background A drawable to use as the background. 
android:clickable Defines whether this view reacts to click events.
android:contentDescription Defines text that briefly describes content of the view.
android:id Supply an identifier name for this view, to later retrieve it with View.findViewById() or Activity.findViewById().
android:isScrollContainer Set this if the view will serve as a scrolling container, meaning that it can be resized to shrink its overall window so that there will be space for an input method.
android:minHeight Defines the minimum height of the view.
android:minWidth Defines the minimum width of the view.
android:onClick Name of the method in this View’s context to invoke when the view is clicked.
android:padding Sets the padding, in pixels, of all four edges.
android:scrollbars Defines which scrollbars should be displayed on scrolling or not.

来自ViewGroup

Attributes

Description

android:addStatesFromChildren Sets whether this ViewGroup’s drawable states also include its children’s drawable states.
android:animateLayoutChanges Defines whether changes in layout should cause a LayoutTransition to run.
android:clipChildren Defines whether a child is limited to draw inside of its bounds or not.
android:clipToPadding Defines whether the ViewGroup will clip its children and resize any EdgeEffect to its padding, if padding is not zero.
android:layoutAnimation Defines the layout animation to use the first time the ViewGroup is laid out.
android:layoutMode Defines the layout mode of this ViewGroup.
android:splitMotionEvents Sets whether this ViewGroup should split MotionEvents to separate child views during touch event dispatch.

方法

本示例演示使用Kotlin在Android中创建ScrollView所涉及步骤。

步骤1:创建一个新项目

  1. 单击文件,然后单击新建=>新建项目。
  2. 为项目模板选择“空活动”。
  3. 选择语言作为Kotlin。
  4. 根据需要选择最小的SDK。

步骤2:修改字符串.xml

字符串.xml文件中添加一些字符串,以在应用程序中显示这些字符串。

strings.xml

    gfgapp_scrollview
    Kotlin is a statically typed,
                 general-purpose programming language developed
                 by JetBrains, that has built world-class IDEs 
                 like IntelliJ IDEA, PhpStorm, Appcode, etc.
                 It was first introduced by JetBrains in 2011 
                 and a new language for the JVM. Kotlin is 
                 object-oriented language, and a “better language” 
                 than Java, but still be fully interoperable
                 with Java code. Kotlin is sponsored by Google, 
                 announced as one of the official languages for 
                 Android Development in 2017. 
                 Advantages of Kotlin language:
                 Easy to learn – Basic is almost similar to java.
                 If anybody worked in java then easily understand
                 in no time. Kotlin is multi-platform – Kotlin is
                 supported by all IDEs of java so you can write 
                 your program and execute them on any machine
                 which supports JVM. It’s much safer than Java.
                 It allows using the Java frameworks and libraries 
                 in your new Kotlin projects by using advanced 
                 frameworks without any need to change the whole
                 project in Java. Kotlin programming language, 
                 including the compiler, libraries and all the
                 tooling is completely free and open source and
                 available on github. Here is the link for 
                 Github https://github.com/JetBrains/kotlin 
                 Applications of Kotlin language:
                 You can use Kotlin to build Android Application. 
                 Kotlin can also compile to JavaScript, and making
                 it available for the frontend. It is also designed 
                 to work well for web development and server-side 
                 development.Kotlin is a statically typed, general-purpose
                 programming language developed by JetBrains that 
                 has built world-class IDEs like IntelliJ IDEA, 
                 PhpStorm, Appcode, etc. It was first introduced
                 by JetBrains in 2011.Kotlin is object-oriented 
                 language and a better language than Java, but still
                 be fully interoperable with Java code. A constructor
                 is a special member function that is invoked when 
                 an object of the class is created primarily to initialize
                 variables or properties. A class needs to have a constructor
                 and if we do not declare a constructor, then the compiler
                 generates a default constructor.
                 Kotlin has two types of constructors –
                 Primary Constructor
                 Secondary Constructor
                 A class in Kotlin can have at most one primary
                 constructor, and one or more secondary constructors. 
                 The primary constructor
                 initializes the class, while the secondary 
                 constructor is used
                 to initialize the class and introduce some extra logic.
                 Explanation:
                 When we create the object add for the class then 
                 the values 5 and 6
                 passes to the constructor. The constructor 
                 parameters a and b 
                 initialize with the parameters 5 and 6 respectively.
                 The local variable c contains the sum of variables. 
                 In the main, we access the property of 
                 contructor using ${add.c}.
                 Explanation:
                 Here, we have initialized the constructor 
                 parameters with some
                 default values emp_id = 100 and emp_name = “abc”.
                 When the object emp is created we passed the values for
                 both the parameters so it prints those values.
                 But, at the time of object emp2 creation, 
                 we have not passed
                 the emp_name so initializer block uses 
                 the default values and
                 print to the standard output.


activity_main.xml


  
    
  
        
    


MainActivity.kt
package com.example.gfgapp_scrollview
  
import androidx.appcompat.app.AppCompatActivity 
import android.os.Bundle
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}


步骤3:修改activity_main.xml

添加了滚动和滚动型内添加一个TextView用来显示被拍摄的字符串.xml文件中的字符串。

activity_main.xml



  
    
  
        
    

如上所述,Scrollview只能包含一个直接子级。在这种情况下,子级是textview。注意到此textview后,您将意识到在textview中添加的文本被称为@ 字符串 / scrolltext ,它表示字符串.xml文件中的字符串资源。

步骤4:MainActivity.kt文件

MainActivity.kt文件无关,因此请保持原样。

MainActivity.kt

package com.example.gfgapp_scrollview
  
import androidx.appcompat.app.AppCompatActivity 
import android.os.Bundle
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

输出:在模拟器上运行

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!