I use data building in my application and I'm new to this. I can't change the layout_marginLeft code. I have seen many examples but they do not work for me. Here is my code in the model.
@BindingAdapter("android:layout_marginLeft")
public static void setLayoutMarginLeft(View view, float marginLeft) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins((int) marginLeft, p.topMargin, p.rightMargin, p.bottomMargin);
view.requestLayout();
}
}
xml file
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="com.aofled.test.window.activity.sleep.SleepActivityViewModel" />
</data>
<TextView
android:id="@+id/tv_sleep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap to screen....."
android:textColor="@color/selector_inactive"
android:textSize="48sp"
android:layout_marginLeft="@{viewModel.layout_marginLeft}"
/>
I cannot build the project. I wrote:
****/ data binding error ****msg:Could not find accessor com.aofled.test.window.activity.sleep.SleepActivityViewModel.layout_marginLeft file:C:\!work\kohote_ch_android 4\kohotehc_android\client\src\main\res\layout\activity_sleep.xml loc:26:45 - 26:71 ****\ data binding error ****
What should I change to make it work?
Well, I found a solution for my problem. I hope someone will help.
My .xml
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="com.kohote.homecare.window.activity.sleep.SleepActivityViewModel" />
</data>
<TextView
android:id="@+id/tv_sleep"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sleep_mode_text"
android:textColor="@color/selector_inactive"
android:textSize="48sp"
app:layout_marginLeft="@{viewModel.marginLeft}"
/>
My SleepActivityViewModel
public ObservableField<Float> marginLeft = new ObservableField<>();
@BindingAdapter({"bind:layout_marginLeft"})
public static void setLayoutMarginLeft(TextView view, float marginLeft) {
if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
p.setMargins((int) marginLeft, p.topMargin, p.rightMargin, p.bottomMargin);
view.requestLayout();
}
}
and my Setter
marginLeft.set(100f, 900f);
This is a working version.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.