📜  Kotlin中的动态ScrollView

📅  最后修改于: 2021-05-08 20:54:10             🧑  作者: Mango

Android中, ScrollView在其内部合并了多个视图,并允许它们进行滚动。

在本文中,我们将讨论如何在Kotlin中以编程方式创建Scroll视图。

让我们首先在Android Studio中创建一个项目。为此,请按照以下说明进行操作:

  • 点击File ,然后依次单击New和New Project并命名。
  • 然后,选择Kotlin语言支持,然后单击下一步按钮。
  • 选择最低的SDK ,无论您需要什么。
  • 选择清空活动,然后单击完成

修改activity_main.xml文件

第二步是设计布局页面。在这里,我们将使用LinearLayout从Kotlin文件获取Scroll View。



  
  
  

添加图像

我们将需要在应用程序中使用一些图像。您可以使用所需的图像,但是需要将图像从我们的本地计算机路径复制到app / res / drawable文件夹。

在MainActivity.kt文件中创建滚动视图

打开app / src / main / Java/yourPackageName/MainActivity.kt。在此文件中,我们声明一个变量ScrollView来创建Scroll View小部件,如下所示:

val scrollView = ScrollView(this)
        val layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 
        ViewGroup.LayoutParams.MATCH_PARENT)
        scrollView.layoutParams = layoutParams

然后使用此将小部件添加到布局中

linearLayout1?.addView(scrollView)
package com.geeksforgeeks.myfirstkotlinapp 
  
import androidx.appcompat.app.AppCompatActivity 
import android.os.Bundle
import android.view.Gravity
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ScrollView
   
class MainActivity : AppCompatActivity() {
   
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
   
        val scrollView = ScrollView(this)
        val layoutParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT)
        scrollView.layoutParams = layoutParams
   
        val linearLayout = LinearLayout(this)
        val linearParams = LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT)
        linearLayout.orientation = LinearLayout.VERTICAL
        linearLayout.layoutParams = linearParams
   
        scrollView.addView(linearLayout)
   
        val imageView1 = ImageView(this)
        val params1 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        //setting margin 
        params1.setMargins(0, 30, 0, 30)
        //aligning the layout to center of the screen
        params1.gravity = Gravity.CENTER
        imageView1.setLayoutParams(params1)
        //setting our own downloaded/custom image to the imageView
        imageView1.setImageResource(R.drawable.image1)
        linearLayout.addView(imageView1)
   
        val imageView2 = ImageView(this)
        val params2 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        params2.setMargins(0, 0, 0, 30)
        params2.gravity = Gravity.CENTER
        imageView2.setLayoutParams(params2)
        imageView2.setImageResource(R.drawable.image2)
        linearLayout.addView(imageView2)
   
        val imageView3 = ImageView(this)
        val params3 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        params3.setMargins(0, 0, 0, 30)
        params3.gravity = Gravity.CENTER
        imageView3.setLayoutParams(params3)
        imageView3.setImageResource(R.drawable.image3)
        linearLayout.addView(imageView3)
   
        val imageView4 = ImageView(this)
        val params4 =
            LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
            ViewGroup.LayoutParams.WRAP_CONTENT)
        params4.setMargins(0, 0, 0, 30)
        params4.gravity = Gravity.CENTER
        imageView4.setLayoutParams(params4)
        imageView4.setImageResource(R.drawable.image4)
        linearLayout.addView(imageView4)
   
        val linearLayout1 = findViewById(R.id.layout)
        linearLayout1?.addView(scrollView)
    }
}

AndroidManifest.xml文件



  
    
        
            
                
  
                
            
        
    
  

作为仿真器运行:

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