📜  Kotlin中的DynamicHorizontal ScrollView

📅  最后修改于: 2021-05-13 18:13:56             🧑  作者: Mango

Android ScrollView允许滚动位于父视图组中的多个视图。可以通过两种方式在android应用程序中垂直或水平滚动。

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

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

  • 单击“文件” ,然后依次单击“新建”和“新建项目”,并根据自己的喜好命名
  • 然后,选择Kotlin语言支持,然后单击下一步按钮。
  • 选择最低的SDK,无论您需要什么。
  • 选择清空活动,然后单击完成。

修改activity_main.xml文件

在这里,我们将使用RelativeLayout从Kotlin文件获取Scroll View。



  
    
  
   

更新字符串.xml文件


    DynamicHorizontal ScrollView

新增图片

我们需要添加一些可用于滚动目的的图像。因此,我们必须将图像从本地计算机路径复制到app / res / drawable文件夹。

在MainActivity.kt文件中创建水平ScrollView

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

val horizontalScrollView = HorizontalScrollView(this)
    //setting height and width
    val layoutParams = LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 
                        ViewGroup.LayoutParams.MATCH_PARENT)
    horizontalScrollView.layoutParams = layoutParams

然后,使用此小部件在布局中添加

val linearLayout1 = findViewById(R.id.layout)
       linearLayout1?.addView(horizontalScrollView)
package com.geeksforgeeks.myfirstkotlinapp
  
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.HorizontalScrollView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
  
class MainActivity : AppCompatActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
  
        val horizontalScrollView = HorizontalScrollView(this)
        //setting height and width
        val layoutParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT)
        horizontalScrollView.layoutParams = layoutParams
  
        val linearLayout = LinearLayout(this)
        //setting height and width
        val linearParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        linearLayout.layoutParams = linearParams
  
        //adding horizontal scroll view to the layout
        horizontalScrollView.addView(linearLayout)
  
        val image1 = ImageView(this)
        //setting height and width
        val params1 = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        image1.layoutParams = params1
        //accessing images that we downloaded and copied to
        // drawable folder and setting it to imageview
        image1.setImageResource(R.drawable.img1)
        linearLayout.addView(image1)
  
        val image2 = ImageView(this)
        //setting height and width
        val params2 = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        image2.layoutParams = params2
        //accessing images that we downloaded and copied to 
        // drawable folder and setting it to imageview
        image2.setImageResource(R.drawable.img2)
        linearLayout.addView(image2)
  
        val image3 = ImageView(this)
        //setting height and width
        val params3 = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        image3.layoutParams = params3
        //accessing images that we downloaded and copied to 
        // drawable folder and setting it to imageview
        image3.setImageResource(R.drawable.img3)
        linearLayout.addView(image3)
  
        val image4 = ImageView(this)
        //setting height and width
        val params4 = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        image4.layoutParams = params4
        //accessing images that we downloaded and copied to 
        // drawable folder and setting it to imageview
        image4.setImageResource(R.drawable.img4)
        linearLayout.addView(image4)
  
        val image5 = ImageView(this)
        //setting height and width
        val params5 = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        image5.layoutParams = params5
        //accessing images that we downloaded and copied to
        // drawable folder and setting it to imageview
        image5.setImageResource(R.drawable.img5)
        linearLayout.addView(image5)
  
  
        //accessing the relative layout where the scrollview will be active
        val linearLayout1 = findViewById(R.id.layout)
        linearLayout1?.addView(horizontalScrollView)
  
    }
}

AndroidManifest.xml文件



  

    
        
            
  
            
        
    

  

作为仿真器运行:

在这里,是在android应用程序中水平滚动的视频。

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