【发布时间】:2017-10-01 19:56:17
【问题描述】:
我已阅读以下Android include layout dynamically with data-binding library 问题。我的问题有点不同。
我有两个用于 Activity 的 xml:
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="myapplication.MainActivityViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="myapplication.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
</layout>
还有一个框架:
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="myapplication.FrameViewModel"/>
</data>
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TEST"/>
</FrameLayout>
</layout>
我想通过数据绑定将 frame.xml 动态扩展为活动 FrameLayout。换句话说,我想在活动中膨胀不同的帧,就像片段一样,没有片段。 我在 Activity 中尝试了以下操作:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
ViewGroup view = activityMainBinding.main;
FrameBinding frameBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.frame,view, false);
}
使用该代码,我可以在 Activity 中显示两种布局。我只能看到活动布局。我应该如何更改我的代码才能在 Activity 中看到我的 Activity 布局视图元素和我的框架视图元素?
【问题讨论】:
标签: android android-activity data-binding android-inflate