【发布时间】:2019-10-21 14:35:53
【问题描述】:
我设法在我的包含中添加了一个参数,以便我可以重用配置略有不同的布局。但它没有显示在 Android Studio 的预览中(我不知道 Android 是否能够做到这一点),而且我也有膨胀它的问题。我不想将它连接到活动,因为我想创建视图的位图,所以我需要手动填充和绘制它。
包含的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="myText" type="String"/>
</data>
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginTop="27dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal"
>
<TextView
android:id="@+id/months"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{myText}" />
</LinearLayout>
</layout>
在这里使用:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<include
layout="@layout/counter_4_1"
bind:myText="@{`Hello World`}" />
</FrameLayout>
</layout>
这在没有数据绑定的情况下工作:
View checkIds = LayoutInflater.from(context).inflate(R.layout.mylayout, null);
((TextView) checkIds.findViewById(viewId)).setText(text);
int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED);
checkIds.measure(specWidth, specWidth);
checkIds.layout(0, 0, checkIds.getMeasuredWidth(), checkIds.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap( checkIds.getMeasuredWidth(), checkIds.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
checkIds.draw(c);
我尝试了数据绑定
View checkIds = DataBindingUtil.inflate(LayoutInflater.from(context),Tools.getLayoutResource(context,countdownSettings.countdownStyle), null,false).getRoot();
我还在 app build.gradle 中启用了 dataBinding {enabled = true}。
但是数据没有显示在视图上。
【问题讨论】:
-
这行
Tools.getLayoutResource(context,countdownSettings.countdownStyle)是否返回您的布局资源ID? -
是的,它从文本字符串中获取
标签: android layout data-binding include