美文网首页
Android中如何用代码方式创建视图

Android中如何用代码方式创建视图

作者: cofbro | 来源:发表于2022-07-17 22:22 被阅读0次

前言

大部分时候,我们是在 xml 文件中创建视图和布局的,但有些时候我们不得不在代码中动态创建视图并显示。

动态创建视图

动态创建视图有几种情况,一种是利用 xml 中的容器,往里面添加 view ,第二种是没有 xml 文件,一切需要用代码创建,第三种是 xml 文件中保存的是具体的 ImageView , Button , TextView 之类的控件 ,我们需要将其解析成 view 并加到界面上去。

1.xml中有容器的情况下

首先是要在 xml 中有根容器

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/root"
    android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>

然后在代码中添加下面这些

//首先创建一个ImageView
val imageView = ImageView(this).apply {
            //在创建ImageView的同时,设置它所要显示的图片
            setImageResource(R.drawable.ic_launcher_foreground)
        }
        //设置控件的必要参数,参数都是在这里设置
        val layoutParams = ConstraintLayout.LayoutParams(
            //设置宽高为300px
            300,300
        ).apply {
            /**设置layoutParams的同时
            *  将它相对于父容器的位置确定
            */
            startToStart = R.id.root
            endToEnd = R.id.root
            topToTop = R.id.root
            bottomToBottom = R.id.root
        }
        //最后将这个视图添加到容器中
        val root = findViewById<ConstraintLayout>(R.id.root)
        root.addView(imageView,layoutParams)

2.没有xml文件时

用代码动态创建视图时,我们有时是没有xml文件的,那么在Activity的 onCreate() 方法中的 setContentView() 中,需要与绑定的就不再是 xml 文件了,而是我们自己创建的容器。

a.首先需要在 values 文件下新建一个 id.xml 文件用来储存各种 id

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <item name="root_id" type="id"/>
</resources>

b.在代码中添加一个容器,并与 Activity 绑定

//因为是根容器,它自动填满整个屏幕
val constraintLayout = ConstraintLayout(this).apply {
  id = R.id.root_id
}
setContentView(constraintLayout)

c.向根容器中添加子控件

//这里的方式就和第一种创建视图的代码一致
val view = View(this).apply {
            setBackgroundColor(Color.BLACK)
        }
        val constraintLayoutParams = ConstraintLayout.LayoutParams(
            100,100
        ).apply {
            startToStart = R.id.root_id
            endToEnd = R.id.root_id
            topToTop = R.id.root_id
            bottomToBottom = R.id.root_id
        }
        constraintLayout.addView(view,constraintLayoutParams)
    }

3.xml中保存的是独立的view时

当xml文件中是一个个独立的 view 时,如:

//这个xml文件名叫作 activity_main.xml
//这样的好处是可以在xml中直观方便的设置一些属性
<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/root"
    android:background="@color/purple_700"
    android:layout_marginStart="20dp"
    android:layout_marginEnd="20dp"
    android:layout_height="match_parent"/>

接着在 Activity 中将这个 xml 文件中解析成 view,并设置一些布局参数,最后将其添加进容器中。

a.将 xml 解析成 view

//将xml中的控件加到容器中
//LayoutInflater这个类的功能就是将xml转化为view
//可以用 val inflater = LayoutInflater.from(this) 得到inflater
//但是kotlin已经给我们提供了这个inflater(其实内部也是调用上面的方法得到的)
//因此我们能直接用这个layoutInflater
val view = layoutInflater.inflate (
    R.layout.activity_main,
    constraintLayout,
    false
)

inflater() 方法中有三个参数,分别是控件的 布局资源文件容器是否将此控件绑定到容器上,我们通常写 false ,因为绑定之后位置是系统默认的,通常在容器中的位置不是我们预想中的位置,我们还需要给其加一些布局参数。

b.添加布局参数,并添加到容器中

//这些代码同样和前面一致
  val constraintLayoutParams = ConstraintLayout.LayoutParams(
        100,100
  ).apply {
        startToStart = R.id.root_id
        endToEnd = R.id.root_id
        topToTop = R.id.root_id
        bottomToBottom = R.id.root_id
  }
  constraintLayout.addView(view,constraintLayoutParams)

相关文章

网友评论

      本文标题:Android中如何用代码方式创建视图

      本文链接:https://www.haomeiwen.com/subject/ythrirtx.html