Android应用程序正在增长,其中一个重要方面是可重用性。有时,应用程序设计的复杂性会越来越高,在此期间,Android通过
包含标签
这用于包含可重用内容的内容。在主布局中共享另一个布局的内容是一个非常好的主意。
XML
XML
XML
XML
Kotlin
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
var customButton: Button? = null
var customImageView: ImageView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// As custom layout contents are included, we can refer them as normal way
// Main advantage lies here only. even this custom_layout can be reusued in different xml
// and in corresponding activity file, they can be refered and there may be a
// different functionality can be carried out.
// get the reference of custom Layout's Button
customButton = findViewById(R.id.ButtonToInclude) as Button
// get the reference of custom Layout's ImageView
customImageView = findViewById(R.id.imageViewToInclude) as ImageView
// We have clicked on Custom layout button, though it is in separate xml
// because of include tag, it is getting focus here and we can do
// activities as we like
customButton!!.setOnClickListener {
Toast.makeText(applicationContext, "We have clicked on Custom layout button", Toast.LENGTH_LONG).show()
}
}
}
合并标签
XML格式
Note: We can find that
- The ViewStub tag is a little bit different because it is not directly included, and will be loaded only when you actually need it, i.e when you set ViewStub’s visibility to “true”.
- But include and merge tags are directly included and loaded at the beginning itself and mainly helpful for splitting of layouts and reusing them wherever necessary.
包含的属性
Attributes |
Description |
---|---|
id | To uniquely identify an include tag |
layout |
To supply an identifier for the layout resource to include a custom layout in our main layout. |
例子
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。
步骤2:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。
XML格式
步骤3:创建一个新的布局资源文件
转到应用程序> res>布局>右键单击>新建>布局资源文件,并将文件命名为custom_layout 。以下是custom_layout.xml的代码,该文件应与合并内容一起显示。
XML格式
步骤4:使用MainActivity.kt文件
转到MainActivity.kt文件,并参考以下代码。下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
var customButton: Button? = null
var customImageView: ImageView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// As custom layout contents are included, we can refer them as normal way
// Main advantage lies here only. even this custom_layout can be reusued in different xml
// and in corresponding activity file, they can be refered and there may be a
// different functionality can be carried out.
// get the reference of custom Layout's Button
customButton = findViewById(R.id.ButtonToInclude) as Button
// get the reference of custom Layout's ImageView
customImageView = findViewById(R.id.imageViewToInclude) as ImageView
// We have clicked on Custom layout button, though it is in separate xml
// because of include tag, it is getting focus here and we can do
// activities as we like
customButton!!.setOnClickListener {
Toast.makeText(applicationContext, "We have clicked on Custom layout button", Toast.LENGTH_LONG).show()
}
}
}
在仿真器上运行代码
我们可以获取视频中附带的输出。必要时应用