【问题标题】:Using includes and data binding for reusing layout does not work使用包含和数据绑定来重用布局不起作用
【发布时间】: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


【解决方案1】:

在包含的子布局中再添加一个变量以传递字符串

你的包含布局应该是这样的:

    <data>
        <variable name="myText" type="String"/>
    </data>
     <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center">

               <include
                   layout="@layout/counter_4_1"
                   bind:myText='@{myText ?? ""}' />
    </FrameLayout>

检查空@{myText??""

如果还有问题请告诉我。

【讨论】:

  • 空指针异常只是因为我没有用包围其他一些视图。所以 DataBindingUtil 现在正在加载视图,但文本仍未显示。我发现您正在使用带有“??”的 lambda 操作。我可能需要在我的 gradle 文件中激活 lambda 才能让它工作。但 Android 表示 lambda 已过时。有没有没有 lambda 的方法?该解决方案是否也会在预览中显示?我猜不是。
  • 我也使用了 bind:myText="@{@string/hello_world}" 编译得很好,但仍然没有显示在视图中。
【解决方案2】:

您还需要在主布局中使用相同的数据变量,在其中调用

    <?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">
    <data>
        <variable name="myText" type="String"/>
    </data>
    <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center">
            <include
                 layout="@layout/counter_4_1"
                 bind:myText="@{myText}" />
    </FrameLayout>
</layout>

然后您可以传递相同的变量,即从您的活动中传递。

public class UserDetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        YourMainLayout mBinding = DataBindingUtil
                .setContentView(this, R.layout.your_main_layout);

        String myText = "Hello World";

        mBinding.setMyText(myText);
    }
}

【讨论】:

  • 我想在父活动中设置变量。所以包含的布局可能会略有不同,比如更大的文本、不同的字体颜色等。
  • 对不起,我的意思是父布局,而不是活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 2017-08-27
  • 2017-01-19
相关资源
最近更新 更多