[来自片段中变量的数据绑定,android?

问题描述 投票:0回答:1

我的片段中有一个字符串变量,我想在XML中将其与数据绑定一起使用这是我的片段

class HomeFragment : Fragment() {

var struc: String = "Organization Structure"

lateinit var binding: FragmentHomeBinding
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    binding = FragmentHomeBinding.inflate(inflater, container, false)
    return binding.getRoot()
}}

这是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">

<data>
    <variable
        name="data"
        type="com.mountmeru.view.HomeFragment" />
</data>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.HomeFragment">
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tvOrganization"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/ivOrganization"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="@{data.struc}"
        android:textAlignment="center"
        android:textColor="@color/mountmerured"
        android:textSize="15sp" />
</FrameLayout></layout>

当我运行应用程序时,永远不会设置textview文本。

这里怎么了?

android kotlin android-databinding
1个回答
0
投票
我认为您在布局膨胀时弄错了。请尝试以下代码

override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment binding = DataBindingUtil.inflate(inflater, layoutId, container, false) return binding.getRoot() }}

© www.soinside.com 2019 - 2024. All rights reserved.