当前位置: 代码迷 >> 综合 >> Android DataBinding 学习(二)
  详细解决方案

Android DataBinding 学习(二)

热度:33   发布时间:2023-12-06 01:28:30.0

dataBinding(二)

【1】在布局中使用vm变量进行资源判断

场景:点击按钮,对应的圆和按钮本身的背景颜色发生改变,不需要单独在代码中设置其背景色,可以直接在布局中镶嵌

 

VM:

public  class TestVM {public ObservableBoolean isGreenGradient = new ObservableBoolean();
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"><data><import type="android.view.View" /><variablename="viewModel"type="com.tzbc.databindtest.TestVM" /></data><LinearLayoutandroid:id="@+id/linearLayout"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:gravity="center"android:orientation="horizontal"><ImageViewandroid:id="@+id/ivGradient"android:layout_width="150dp"android:layout_height="150dp"android:background="@{viewModel.isGreenGradient ? @drawable/green_gradient : @drawable/yellow_gradient}" /><Buttonandroid:id="@+id/btGradient"android:layout_width="wrap_content"android:layout_height="50dp"android:layout_marginLeft="20dp"android:background="@{viewModel.isGreenGradient ? @drawable/button_green_bg : @drawable/button_yellow_bg}"android:paddingLeft="10dp"android:paddingRight="10dp"android:text="@string/gradientText"android:textColor="@color/yellowEnd"android:textSize="12sp" /></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginLeft="20dp"android:layout_marginTop="30dp"android:layout_marginRight="20dp"android:background="@drawable/view_line" /></LinearLayout>
</layout>

activity:

package com.tzbc.databindtest;import android.os.Bundle;
import android.view.View;import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;import com.tzbc.databindtest.databinding.ActivityMainBinding;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final String TAG = "tzbc";private TestVM testVM;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);testVM = new TestVM();testVM.isGreenGradient.set(true);activityMainBinding.setViewModel(testVM);activityMainBinding.btGradient.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btGradient:testVM.isGreenGradient.set(!testVM.isGreenGradient.get());break;}}}

资源文件:

string:

<string name="gradientText">click to change gradient</string>
<string name="gradientGreen">Green</string>
<string name="gradientYellow">Yellow</string>

view_line:

<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@color/black"/></shape>
<color name="black">#1A000000</color>
<color name="buttonBg">#569DA7</color>
<color name="buttonBg1">#F1A12B</color>
<color name="greenStart">#15611B</color>
<color name="greenMiddle">#60C964</color>
<color name="greenEnd">#83CC30</color>
<color name="yellowStart">#FF9800</color>
<color name="yellowMiddle">#E6CE08</color>
<color name="yellowEnd">#FAED82</color>
green_gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><gradientandroid:endColor="@color/greenEnd"android:startColor="@color/greenStart" />
</shape>
yellow_gradient:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><gradientandroid:endColor="@color/yellowEnd"android:startColor="@color/yellowStart" />
</shape>

button_green_bg:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@color/buttonBg" /><corners android:radius="20dp" />
</shape>
button_yellow_bg:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="@color/buttonBg1" /><corners android:radius="20dp" />
</shape>

 

 

 

 

 

  相关解决方案