首先,在您的应用程序级别中放入以下代码行
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
testOptions {
unitTests.returnDefaultValues = true
}
kotlinOptions {
jvmTarget = '1.8'
}
//Enable View Bindings to true
buildFeatures {
viewBinding = true
}
}
然后通过右键单击“文件”->新建->片段->选择空白片段(或您想要的任何片段)在您的项目中添加新片段。这将创建您的片段类文件以及与之关联的 xml 文件。
我创建了名为“HomeFragment.kt”的空白片段,其 xml 文件的名称为“fragment_home.xml”。
在我的 xml 文件中,我添加了一个 textview 和 Button(您可以在此处定义您的小部件 - 一种设计布局),代码如下所示。
“fragment_home.xml”
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.textview.MaterialTextView
android:id="@+id/mTv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Home Fragment"
android:textColor="@color/main_black"
android:lineSpacingExtra="@dimen/dimen_2dp"
android:gravity="center" />
<com.google.android.material.button.MaterialButton
android:id="@+id/mBtn_homeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Here!"
android:layout_gravity="center"
android:textSize="@dimen/dimen_20sp"
android:textAllCaps="false"
app:cornerRadius="@dimen/dimen_50dp"
android:paddingLeft="@dimen/dimen_50dp"
android:paddingRight="@dimen/dimen_50dp"
android:paddingTop="12dp"
android:paddingBottom="12dp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
最后在您的片段类中,您可以使用 viewBinding 绑定这些属性。
“家庭片段.kt”
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
// view binding will create an Class for your xml file, you can use it directly by this "FragmentHomeBinding" as my xml file name is "fragment_home.xml"
// create a variable of your xml binding class
private lateinit var binding : FragmentHomeBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (arguments!=null) {
Log.i(tag, "onCreate: ")
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
//val root = inflater.inflate(R.layout.fragment_home, container, false)
binding = FragmentHomeBinding.inflate(inflater, container, false)
// observe your text change by using viewModel
homeViewModel.text.observe(viewLifecycleOwner, Observer {
//you can use your view/widgets by using the binding object like this ---
binding.mTvTitle.text = it
})
// button click event -- using viewBinding
binding.mBtnHomeButton.setOnClickListener {
Toast.makeText(context, "Hello..!", Toast.LENGTH_SHORT).show()
}
return binding.root
}
}
干杯..!