Data Binding Include

2020-05-19  本文已影响0人  Perston
<include layout="@layout/include_demo" app:employee="@{employee}"/>

include_demo.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="employee"
            type="com.mooc.databingdingtest.Employee" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent">


        <!--监听器绑定的语法——表达式语法-->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.firstName}"
            android:textSize="24sp"
            android:gravity="center" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{employee.lastName}"
            android:textSize="24sp"
            android:gravity="center" />
    </LinearLayout>
</layout>

ViewStub

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"/>
</LinearLayout>

activity_demo.xml

        <ViewStub
            android:id="@+id/view_stub"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout="@layout/viewstub"/>

DemoActivity.java 只有在调用了才显示

binding.viewStub.getViewStub().inflate();

ViewStub--使用介绍

上一篇 下一篇

猜你喜欢

热点阅读