我想在我的安卓应用程序中使用碎片(Fragment),但是我无法在我的碎片类中导入FragmentTitleBinding
。我尝试了互联网上所有可能的解决方案。
- 我检查了xml文件中的布局标签
- 我编辑了build.gradle文件
- 同步项目
import com.example.android.navigation.databinding.FragmentTitleBinding
class TitleFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val binding : com.example.android.navigation.databinding.FragmentTitleBinding=
DataBindingUtil.inflate(inflater,R.layout.fragment_title,container,false)
return binding.root;
}
}
片段化XML
<?xml version="1.0" encoding="utf-8"?>
<layout 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"
tools:context="com.example.android.navigation.TitleFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/titleConstraint"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/horizontal_margin"
android:layout_marginEnd="@dimen/horizontal_margin"
android:paddingStart="@dimen/button_padding"
android:paddingEnd="@dimen/button_padding"
android:text="Play"
android:textColor="@color/colorAccent"
android:textSize="@dimen/button_text_size"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/titleImage" />
<ImageView
android:id="@+id/titleImage"
android:layout_width="0dp"
android:layout_height="@dimen/image_header_height"
android:layout_marginStart="@dimen/horizontal_margin"
android:layout_marginEnd="@dimen/horizontal_margin"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="@+id/playButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/android_trivia" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
dataBinding {
enabled = true
}
defaultConfig {
applicationId 'com.example.android.navigation'
minSdkVersion 19
targetSdkVersion 28
vectorDrawables.useSupportLibrary = true
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
buildToolsVersion = '28.0.3'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version_kotlin"
// Constraint Layout
implementation "androidx.constraintlayout:constraintlayout:$version_constraint_layout"
// Core
implementation "androidx.core:core:$version_core"
// Material Design
implementation "com.google.android.material:material:$version_material"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
kapt 'com.android.databinding:compiler:3.3.0'
}
kapt {
generateStubs = true
}
我想解决
FragmentTitleBinding
,但它报错为未解决的引用。
R.layout.fragment_title
。 - Tidder<layout>...</layout>
标签内添加一个<data>
标签,就像文档中所示:https://developer.android.com/topic/libraries/data-binding/expressions.html - Tidder