我已閱讀以下Android include layout dynamically with data-binding library的問題。我的問題有點不同。如何使用數據綁定動態擴展活動中的框架佈局?
我有兩個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與數據綁定。換句話說,我想誇大活動中的不同框架,就像片段一樣,沒有片段。 我試圖在活動如下:
@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);
}
與該代碼我可以得到兩個佈局在活動展示。我只能看到活動佈局。我應該如何更改我的代碼以便能夠在活動中看到我的活動佈局視圖元素和框架視圖元素?
我想顯示動態盟友的框架。 – codeme
@codeme查看更新後的答案。我希望這有幫助。 – Cheticamp
謝謝,非常有幫助。 – codeme